且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么是基类的成员从派生类相同的成员有什么不同?

更新时间:2023-02-15 15:14:34

它们的区别在于它们的 ReflectedType 属性:

They differ in their ReflectedType property:

的ReflectedType属性检索用于获得的MemberInfo的此实例的类型的对象。这可能不同于DeclaringType属性的值,如果再$ P $此的MemberInfo对象psents一个从基类继承的成员。

The ReflectedType property retrieves the Type object that was used to obtain this instance of MemberInfo. This may differ from the value of the DeclaringType property if this MemberInfo object represents a member that is inherited from a base class.

所以,如果你打印出 m1.ReflectedType ,它应该打印。如果你打印出 m2.ReflectedType ,它应该打印

So if you print out m1.ReflectedType, it should print Human. If you print out m2.ReflectedType, it should print Man.

编辑:为什么等于运算实现这样来讲:它始终是一个微妙的设计决策,以制定出什么 == 应该是说,在情况下,有可以是对象之间区分的,而不是主的差异。这是提供不同的的IEqualityComparer 的实施是有用的,但当然,这并不在运营商自己的工作。

In terms of why the equality operator is implemented this way: it's always a delicate design decision to work out what == should mean, in the case where there may be distinguishable but not "primary" differences between objects. This is where providing different IEqualityComparer implementations is useful, but of course that doesn't work for the operators themselves.

在一般情况下,如果 X ==是是真的,那么这是相当不寻常的 x.Foo 是不同的以 y.Foo 的任何财产。我不能马上想到哪里发生在该框架的任何情况。

In general, if x == y is true then it's quite unusual for x.Foo to be different to y.Foo for any property. I can't immediately think of any cases where that occurs in the framework.