为什么是基类的成员从派生类相同的成员有什么不同? [英] Why is a member of base class different from the same member in derived class?

查看:198
本文介绍了为什么是基类的成员从派生类相同的成员有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个随访对这一问题:<一href="http://stackoverflow.com/questions/6658669/lambda-ex$p$pssion-not-returning-expected-memberinfo">Lambda EX pression没有返回预期的MemberInfo

This is a followup to this question: Lambda expression not returning expected MemberInfo

class Human
{
    public string name { get; set; }
}

class Man : Human
{

}

var m1 = typeof(Human).GetProperty("name");
var m2 = typeof(Man).GetProperty("name");

//m1 != m2 why?

这同样适用的MethodInfo 秒。

我可以理解,必须有一个差异,当是<$的接口,或者当名称 C $ C>人是抽象/虚拟。但是,为什么会这样的密封类型?是不是名称 究竟名称

I can understand there has to be a difference when Human is an interface, or when name of Human is abstract/virtual. But why is it so for sealed types? Isn't name of Man exactly name of Human?

澄清:作为乔恩说,他们的 ReflectedType 是不同的。 ReflectedType 在平等<$ C C $>在决定接口成员或重写成员平等的时候,因为它们是不同的来得心应手。但我不认为它应该被视为决定像上面的简单的情况下平等。可能是设计团队希望能保持一致。只是不知道是什么理由促使框架设计者考虑 ReflectedType 的决定同一成员平等横跨多个类属性。

Clarification: As Jon says their ReflectedTypes are different. ReflectedType in equality should come handy when deciding equality of interface members or overridden members since they are different. But I don't think it should be considered for deciding equality of simple cases like above. May be the design team wanted to be consistent. Just wondering what rationale drove framework designers to consider ReflectedType property in deciding equality of same member spanning over multiple classes.

推荐答案

它们的区别在于它们的 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.

这篇关于为什么是基类的成员从派生类相同的成员有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆