7

Here's a simple test demonstrating the problem:

class MyBase { public int Foo { get; set; } } class MyClass : MyBase { } [TestMethod] public void TestPropertyCompare() { var prop1 = typeof(MyBase).GetProperty("Foo"); var prop2 = typeof(MyClass).GetProperty("Foo"); Assert.IsTrue(prop1 == prop2); // fails //Assert.IsTrue(prop1.Equals(prop2)); // also fails } 

I need a comparing method which will determine that these two properties are actually represent the same property. What is the correct way of doing this?

In particular I want to check if property actually comes from base class and not altered in any way like overriden (with override int Foo), hidden (with new int Foo) properties, interface properties (i.e. explicit implementation in derived class ISome.Foo) or any other way that lead to not calling MyBase.Foo when instanceOfDerived.Foo is used.

8
  • 2
    Are you trying to compare their values? or the actual PropertyInfo objects? Commented Apr 22, 2016 at 16:54
  • @dotctor you're right, but I need a method which will show that that they are (in some other sence of equality, which I hope you can understand right) Commented Apr 22, 2016 at 16:57
  • @Mikanikal no, just the PropertyInfo objects. I understand that they're different objects, but the only difference is PropertyInfo.ReflectedType property value, and I want somehow ignore this Commented Apr 22, 2016 at 16:59
  • @astef please try to clarify what equality you expect. It is somewhat clear that you are interested in types being somewhat similar, but you need to clarify exact criteria. I.e. comparing interface's property to concrete type's could be more complicated... And don't forget that regular definition of "A equals B" implies that "if A equals B and B equals C than A equals C" which may not work for interface+2 concrete classes case - so you probably looking for "similar", not "equals". Commented Apr 22, 2016 at 17:00
  • How do you determine that two properties are equal? Commented Apr 22, 2016 at 17:02

1 Answer 1

8

ReflectedType always returns the type that you doing reflection on. DeclaringType, on the other hand, tells you which type the property is actually declared on. So what you need to do is this:

public static class TypeExtensions { public static bool PropertyEquals(this PropertyInfo property, PropertyInfo other) { return property.DeclaringType == other.DeclaringType && property.Name == other.Name; } } 

Usage:

var prop1 = typeof(MyBase).GetProperty("Foo"); var prop2 = typeof(MyClass).GetProperty("Foo"); var isSame = prop1.PropertyEquals(prop2); //will return true 

Edit: Removed The PropertyType check as suggestion from @Rob in the comments.

Sign up to request clarification or add additional context in comments.

2 Comments

Very clean solution. However, I believe (correct me if I'm wrong), that you only need to check DeclaringType and Name. You can't declare properties with the same name on the same type, even if they are of different types.
I would check on PropertyInfo.DeclaringType and PropertyInfo.MetadataToken. So there can be no edge case don't think of yet.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.