Check if PInvoke method is used

Is there a way to check if the method uses PInvoke? I iterate over all the methods in the assembly using MethodBase, but I want to check if the method uses PInvoke. Here is the code I'm using:

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

Also, if possible, how can there be a way that I can check the DLL used and the function / entry point being called?

+4
source share
2 answers

You can check if the DllImportAttribute method is decorated . If so, it uses PInvoke.

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

You can use this extension method:

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

Source: https://habr.com/ru/post/1437232/


All Articles