Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if a method is 'extern' using reflection

Tags:

c#

reflection

How do I determine if a method is extern , using reflection?

Sample method:

var mEnter = typeof(System.Threading.Monitor).GetMethod("Exit", BindingFlags.Static | BindingFlags.Public);
like image 483
animaonline Avatar asked Dec 16 '22 04:12

animaonline


2 Answers

var isExtern = (mEnter.MethodImplementationFlags
                    & MethodImplAttributes.InternalCall) != 0;
like image 66
Marc Gravell Avatar answered Feb 17 '23 06:02

Marc Gravell


I don't know is any direct way to figure out but I can show a trick that I used Suppose we have a class contains extern method

class MyClass
{
    [DllImport("kernel32.dll")]
    public static extern bool Beep(int ferequency, int duration);
    public static void gr()
    {
    }
    public void GG()
    {
    }
}

we can get check extern method by writting this code

 var t = typeof(MyClass);
        var l = t.GetMethods();
        foreach (var item in l)
        {
            if (item.GetMethodBody() == null && item.IsStatic)
            {
                // Method is Extern
            }
        }
like image 31
DeveloperX Avatar answered Feb 17 '23 06:02

DeveloperX