Equality for .NET PropertyInfos

杀马特。学长 韩版系。学妹 提交于 2019-12-04 05:11:38

I'm guessing they have a different ReflectedType. For example, inheritance:

class A {
   public int Foo {get;set;}
}
class B : A {}

now look at typeof(A).GetProperty("Foo") and typeof(B).GetProperty("Foo").

Object identity is only promised for the Type class, not for the other reflection classes. A possibly sound way to compare for equality is to check that the properties have the same metadata token and came from the same module. So try this:

bool equal = prop1.MetadataToken == prop2.MetadataToken &&
             prop1.Module.Equals(prop2.Module);

Which makes sense as long as ecma 335 applies. I could not test this against your code since you didn't post it. So just try it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!