C# 检查该方法是否使用PInvoke

C# 检查该方法是否使用PInvoke,c#,pinvoke,system.reflection,methodbase,C#,Pinvoke,System.reflection,Methodbase,是否有检查方法是否使用PInvoke的方法? 我正在使用MethodBase遍历程序集中的所有方法,但我想检查该方法是否使用PInvoke。 以下是我正在使用的代码: foreach (MethodBase bases in mtd.GetType().GetMethods()) { //check if the method is using pinvoke } 另外,如果可能的话,我如何检查正在使用的DLL和正在调用的函数/入口点?您可以检查一个方法是否被修饰。如果是这样

是否有检查方法是否使用PInvoke的方法? 我正在使用MethodBase遍历程序集中的所有方法,但我想检查该方法是否使用PInvoke。 以下是我正在使用的代码:

 foreach (MethodBase bases in mtd.GetType().GetMethods())
 {
      //check if the method is using pinvoke
 }

另外,如果可能的话,我如何检查正在使用的DLL和正在调用的函数/入口点?

您可以检查一个方法是否被修饰。如果是这样的话,那就是使用了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);
    }

打开代码分析。它会开始向他们咆哮。:)有问题的方法。如果该方法本身使用私有pinvoke声明会怎么样?这样做的真正意义是什么?它只是为了调试目的:数据应该被标记为正确答案,它要快得多。