Same for methods too:
I am given two instances of PropertyInfo or methods which have been extracted from the class they sit on via GetProperty()
or GetMember()
etc, (or from a MemberExpression maybe).
I want to determine if they are in fact referring to the same Property or the same Method so
(propertyOne == propertyTwo)
or
(methodOne == methodTwo)
Clearly that isn't going to actually work, you might be looking at the same property, but it might have been extracted from different levels of the class hierarchy (in which case generally, propertyOne != propertyTwo
)
Of course, I could look at DeclaringType, and re-request the property, but this starts getting a bit confusing when you start thinking about
- Properties/Methods declared on interfaces and implemented on classes
- Properties/Methods declared on a base class (virtually) and overridden on derived classes
- Properties/Methods declared on a base class, overridden with 'new' (in IL world this is nothing special iirc)
At the end of the day, I just want to be able to do an intelligent equality check between two properties or two methods, I'm 80% sure that the above bullet points don't cover all of the edge cases, and while I could just sit down, write a bunch of tests and start playing about, I'm well aware that my low level knowledge of how these concepts are actually implemented is not excellent, and I'm hoping this is an already answered topic and I just suck at searching.
The best answer would give me a couple of methods that achieve the above, explaining what edge cases have been taken care of and why :-)
Clarification:
Literally, I want to make sure they are the same property, here are some examples
public interface IFoo
{
string Bar { get; set; }
}
public class Foo : IFoo
{
string Bar { get; set; }
}
typeof(IFoo).GetProperty("Bar")
and
typeof(Foo).GetProperty("Bar")
Will return two property infos, which are not equal:
public class BaseClass
{
public string SomeProperty { get; set ; }
}
public class DerivedClass : BaseClass { }
typeof(BaseClass).GetMethod("SomeProperty")
and
typeof(DerivedClass).GetProperty("SomeProperty")
I can't actually remember if these two return equal objects now, but in my world they are equal.
Similarly:
public class BaseClass
{
public virtual SomeMethod() { }
}
public class DerivedClass
{
public override SomeMethod() { }
}
typeof(BaseClass).GetMethod("SomeMethod")
and
typeof(DerivedClass).GetProperty("SomeMethod")
Again, these won't match - but I want them to (I know they're not specifically equal, but in my domain they are because they refer to the same original property)
I could do it structurally, but that would be 'wrong'.
Further Notes:
How do you even request the property that's hiding another property? Seems one of my earlier suppositions was invalid, that the default implementation of GetProperty("name")
would refer to the current level by default.
BindingFlags.DeclaringType
appears just to end up returning null!