7

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

I am doing this by iterating through all the type in the smaller dll and checking if each method in them also exists (name and parameter list) in the larger dll.

The problem is that Assembly.GetMethods() returns both methods and property getters/setters, which I guess, yeah, are a kind of method, but this is bad for me in this circumstance.

So, my question is, how can I tell if a MethodInfo object stores a property or a real method?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
nestedloop
  • 2,596
  • 23
  • 34
  • Looks like a dupe of http://stackoverflow.com/questions/7819489/identify-whether-a-methodinfo-instance-is-a-property-accessor ? – AakashM Jun 06 '12 at 08:21
  • @AakashM: It seems so indeed, unfortunately I did not find that before and was in a bit of a hurry, thanks for pointing that out. Is there anything I have to do in cases like this? – nestedloop Jun 06 '12 at 11:28

4 Answers4

13

Property accessors are marked as specialname by the compiler. You can check this with MethodBase.IsSpecialName, which you can check on your MethodInfo object. This property is also true for other special methods, such as operator overloads.

Another possibility to exclude property getters and setters would be this query:

from m in typeof(SomeType).GetMethods()
where !typeof(SomeType).GetProperties().Any(p => p.GetGetMethod() == m || p.GetSetMethod() == m)
select m;
Botz3000
  • 39,020
  • 8
  • 103
  • 127
  • 1
    Yes, that detects property accessors, but property accessors aren't the only methods that are marked as SpecialName, so it will give false positives. –  Jun 06 '12 at 08:20
  • +1 for working out your second suggestion, although I'd consider calling `GetGetMethod(nonPublic: true)` and similarly for the setter and the `GetMethods`/`GetProperties` calls themselves. –  Jun 06 '12 at 08:32
  • 1
    Your first solution works well enough for me, as I need only the "pure" methods, so I have to exclude everything else. I thank you very much indeed, sir! :D – nestedloop Jun 06 '12 at 09:12
2

A property accessor is a method. A property is nothing more than syntactic shorthand for a get/set accessor.

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

could have been written, if C# were defined differently, as

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

If you understand that, you may see that a MethodInfo cannot tell you the answer you're looking for. Instead, you have to take the opposite path: see if there are any properties (and perhaps events too) that use that method as an accessor. You could use Type.GetProperties() and a simple foreach loop.

  • Yep, just got that. Thanks for clearing that up. I have started with Reflection yesterday and this is due today, so was in a hurry. Cheers. – nestedloop Jun 06 '12 at 08:33
0

Didn't try, but this should help :

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

More on this : MemberTypes enumeration and MethodInfo.MemberType property

Steve B
  • 36,818
  • 21
  • 101
  • 174
  • Thanks for the quick answer. I have tried several things with MemberTypes, and it seems that all of them are seen as methodInfo.MemberType == MemberTypes.Method. I guess that's cause they are implicit properties... – nestedloop Jun 06 '12 at 08:29
-1

Try the following,

methodInfo.IsDefined(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute), false);
methodinfo.MemberType == MemberTypes.Property
Echilon
  • 10,064
  • 33
  • 131
  • 217
Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42
  • 1
    @RajeshSubramanian Why would either of your suggestions help? I'm not saying you're just randomly guessing, I'm saying I don't understand why you're suggesting them. –  Jun 06 '12 at 08:19