1

Possible Duplicate:
Determine if Equals() is an override?

I need to run specific pieces of code for whether a specific type overrides Object.Equals or not.

How can I check if a type overrides this method?

Typing typeof(mytype).GetMethod("Equals"). in VS brings me a wide list of options, but there's no "IsOverriden" or something like that.

Community
  • 1
  • 1
Conrad Clark
  • 4,533
  • 5
  • 45
  • 70
  • 1
    The point of polymorphism is that you don't have to care whether a class overrides methods like `Equals` -- if it does, then you trust it to do the right thing. To even care, smells. – cHao Dec 27 '11 at 16:36
  • Do you want to know if *that* type overrides the method, or if any of its base classes overrides? – Gabe Dec 27 '11 at 16:36
  • @cHao he could be crafting a framework that uses reflection over types that need to implement a custom Equals. It is common in WPF. – Louis Kottmann Dec 27 '11 at 16:38
  • @Ani in fact, it sounds like an exact duplicate, should I delete it ? I tried searching before but it seems I failed. – Conrad Clark Dec 27 '11 at 16:38
  • @cHao has a good point, do you really REALLY have to check this? I suppose there are some reasonable use cases but they are rare and far in between, maybe there is a better way to achieve what you are trying to do? – George Mauer Dec 27 '11 at 16:39
  • @Conrad: No don't delete it. The moderators will leave as is/merge/delete as they deem appropriate. – Ani Dec 27 '11 at 16:40
  • Yeah I don't think this should be deleted if it is easier to find. – George Mauer Dec 27 '11 at 16:40
  • @cHao I'm comparing two objects of specific types, I'll call equals if they override it or are primitives/string/date/etc, or compare them memberwise if they do not. – Conrad Clark Dec 27 '11 at 16:44
  • @Conrad: If those specific types can be "equal" valuewise without being *equal* according to `Object.Equals`, then they should override `Equals`. If they don't, then you generally shouldn't care whether they have the same values, because nothing else in the framework will treat them as equal. – cHao Dec 27 '11 at 16:49
  • @cHao I can't find a good way to do this. I'm using roslyn ctp to compare two pieces of code, then crawl inside a method and retrieve a report of all changes. But instead of comparing each object separately, I'm crawling into the objects inside `MethodDeclarationSyntax` and checking if they're equal. – Conrad Clark Dec 27 '11 at 17:00
  • I'm generally no friend of deleting questions, but since this one does not contain a correct answer, and the duplicate does, I think it's appropriate here. – CodesInChaos Dec 27 '11 at 17:50

3 Answers3

1

Check whether DeclaringType is typeof(object).

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Yeah, basically check the DeclaringType of the MethodInfo object returned by GetMethod – George Mauer Dec 27 '11 at 16:38
  • That doesn't help one bit: object has Equals() by default, but it doesn't help to know if it's overriden. – Louis Kottmann Dec 27 '11 at 16:39
  • instance.GetType().GetMethod('MethodName').DeclaringType should help, yes – George Mauer Dec 27 '11 at 16:41
  • @Baboon: Are you sure about that? – jason Dec 27 '11 at 16:42
  • @Baboon: Wrong. The `DeclaringType` for an overriden `Equals` will not be `object`. – SLaks Dec 27 '11 at 16:44
  • @Baboon: From MSDN: "If the Type object from which this MemberInfo object was obtained did not declare this member, the DeclaringType property will represent one of its base types." That means if the the thingie exists in `Object` and no class along the inheritance chain overrides it, `DeclaringType` will return `Object`'s type. – cHao Dec 27 '11 at 16:45
  • You can construct strange types for which this doesn't work. Method hiding is a strange beast. – CodesInChaos Dec 27 '11 at 16:48
1

There are two ways, first the MethodInfo class has a DeclaringType you can use to see if it is System.Object. Second you can use the overloads of GetMethod that accept a BindingFlags enum and pass in DeclaredOnly to ensure you don't get any parent objects.

Guvante
  • 18,775
  • 1
  • 33
  • 64
-1

You can use the binding flags to your advantage:

var t = typeof(mytype).GetMethod(
            "Equals",
            BindingFlags.Public |
            BindingFlags.Instance |
            BindingFlags.DeclaredOnly
        );

Then t is not null if and only if mytype overrides Equals.

jason
  • 236,483
  • 35
  • 423
  • 525
  • 4
    Your code doesn't even work for `string` and most other types that override equals because it hits an `AmbiguousMatchException` because these types overload `Equals`. And it doesn't take care of method hiding either. Check Ani's answer on the duplicate for a method that actually works. – CodesInChaos Dec 27 '11 at 17:30