ReflectedType
始终返回进行反射的类型。
DeclaringType
告诉在哪种类型中声明属性。因此,您的等号支票需要替换为:
public static class TypeExtensions
{
public static bool PropertyEquals(this PropertyInfo property, PropertyInfo other)
{
return property.DeclaringType == other.DeclaringType
&& property.Name == other.Name;
}
}
用法:
var prop1 = typeof(MyBase).GetProperty("Foo");
var prop2 = typeof(MyClass).GetProperty("Foo");
var isSame = prop1.PropertyEquals(prop2); //will return true
编辑:删除了评论中@Rob的PropertyType检查建议。