2012-10-01 37 views
4

有无论如何检查一个方法是否使用PInvoke? 我正在循环使用MethodBase的程序集中的所有方法,但我想检查该方法是否使用PInvoke。 这里是我使用的代码:检查方法是否使用PInvoke

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

而且如果可能的话怎么能有正在使用一种方法,我可以检查DLL和功能/入口点正在被叫什么名字?

+0

打开代码分析。它会开始对他们嚎叫。 :) –

+0

一个可疑的方法。如果该方法本身使用私人密码声明怎么办?做这件事的真实点是什么? –

+0

这只是为了调试目的:D – method

回答

5

您可以检查方法是否用DllImportAttribute装饰。如果是这样,它使用PInvoke。

foreach (MethodBase methodBase in mtd.GetType().GetMethods()) 
{ 
    if (methodBase.CustomAttributes.Any(cad => cad.AttributeType == typeof(DllImportAttribute)) 
    { 
     // Method is using PInvoke 
    } 
} 
1

您可以使用此扩展方法:在

public static bool IsPinvoke(this MethodBase method) 
    { 
     return method.Attributes.HasFlag(MethodAttributes.PinvokeImpl); 
    }