检查该方法是否使用PINVOKE
-
12-12-2019 - |
题
有条不实地检查方法是否使用pinvoke?
我正在使用MethodBase循环通过组件中的所有方法,但我想检查该方法是否使用PinVoke。
这是我使用的代码:
foreach (MethodBase bases in mtd.GetType().GetMethods())
{
//check if the method is using pinvoke
}
.
如果有可能有一种方法,我可以如何检查正在使用的dll和被调用的函数/入口点?
解决方案
您可以检查方法是否用 dllimportattribute 。如果是这样,它使用Pinvoke。
foreach (MethodBase methodBase in mtd.GetType().GetMethods())
{
if (methodBase.CustomAttributes.Any(cad => cad.AttributeType == typeof(DllImportAttribute))
{
// Method is using PInvoke
}
}
. 其他提示
您可以使用此扩展方法:
public static bool IsPinvoke(this MethodBase method)
{
return method.Attributes.HasFlag(MethodAttributes.PinvokeImpl);
}
. 不隶属于 StackOverflow