Determine if a method is 'extern' using re

2019-05-10 12:45发布

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);

标签: c# reflection
2条回答
你好瞎i
2楼-- · 2019-05-10 13:41

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
            }
        }
查看更多
成全新的幸福
3楼-- · 2019-05-10 13:42
var isExtern = (mEnter.MethodImplementationFlags
                    & MethodImplAttributes.InternalCall) != 0;
查看更多
登录 后发表回答