System.Reflection - How to determine if a MethodInfo object is a Method or Property object?

I have two similar (not identical) DLLs, one of which is actually part of the other. I am trying to see if there is still compatibility (that is, if the smaller is still fully included in the larger).

I do this by repeating all types in the smaller dll and checking if each method (name and parameter list) exists in them in the big dll.

The problem is that Assembly.GetMethods () returns both methods and getters / seters properties , which, I think, yes, this is a kind of method, but it is bad for me in this case.

So my question is: how can I determine if a MethodInfo object saves a property or a real method?

+5
source share
4 answers

Object access attributes are marked as specialnamecompiler. You can check this with the help MethodBase.IsSpecialNameyou can check on your object MethodInfo. This property is also valid for other special methods, such as operator overloads.

Another possibility to exclude getters and seters properties is the following query:

from m in typeof(SomeType).GetMethods()
where !typeof(SomeType).GetProperties().Any(p => p.GetGetMethod() == m || p.GetSetMethod() == m)
select m;
+11
source

An accessory of properties is a method. A property is nothing more than a syntax transcript for get / set accessor.

class A
{
    public int P { get { return 0; } }
}

could be written if C # were defined differently since

class A
{
    public int get_P() { return 0; }
    public int P { get: get_P }
}

, , MethodInfo , . : , - (, , ), . Type.GetProperties() foreach.

+2

Try the following:

methodInfo.IsDefined(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute), false);
methodinfo.MemberType == MemberTypes.Property
0
source

Not trying, but this should help:

var isMethod = (yourMethodInfo.MemberType & MemberTypes.Method) == MemberTypes.Method;

More on this: MemberTypes enumeration and MethodInfo.MemberType Property

0
source

All Articles