I am looking for a way to filter out methods that have an unsafe modifier through reflection. This is not like a method attribute.
Is there any way?
EDIT: It seems that this information is not in the metadata, at least I do not see it in IL. However, the reflector shows the unsafe modifier in the C # view. Any ideas on how this is done?
EDIT 2:. For my needs, I received a check that suggests that if one of the method parameters is a pointer or the return type is a pointer, then the method is unsafe.
public static bool IsUnsafe(this MethodInfo methodInfo) { if (HasUnsafeParameters(methodInfo)) { return true; } return methodInfo.ReturnType.IsPointer; } private static bool HasUnsafeParameters(MethodBase methodBase) { var parameters = methodBase.GetParameters(); bool hasUnsafe = parameters.Any(p => p.ParameterType.IsPointer); return hasUnsafe; }
This does not handle, of course, the situation when an unsafe block is executed in a method, but again, all that interests me is the signature of the method.
Thanks!
reflection c # unsafe
Igal tabachnik
source share