4

Is there anyway to check if a method uses PInvoke ? I'm looping through all the methods in an assembly using MethodBase but I want to check if the method is using PInvoke. Here is the code I'm using :

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

Also if it is possible how can is there a way I can check for the DLL being used and the function/entrypoint that is being called?

riQQ
  • 9,878
  • 7
  • 49
  • 66
method
  • 1,369
  • 3
  • 16
  • 29

2 Answers2

5

You can check to see if a method is decorated with DllImportAttribute. If so, it's using PInvoke.

foreach (MethodBase methodBase in mtd.GetType().GetMethods())
{
    if (methodBase.CustomAttributes.Any(cad => cad.AttributeType == typeof(DllImportAttribute))
    {
         // Method is using PInvoke
    }
}
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
2

You can use this extension method:

    public static bool IsPinvoke(this MethodBase method)
    {
        return method.Attributes.HasFlag(MethodAttributes.PinvokeImpl);
    }
ravyoli
  • 698
  • 6
  • 13