据我所知,除了.net中的接口外,所有内容都来自于对象.但是我注意到当我按下“.”在界面名称出现Equals方法之后.当我按F12 for equals方法时,它会指向对象类中的equals方法.如果接口不是从对象类派生的,那么equals方法来自哪里?
解决方法
从C#4规范的第13.2节:
Note that the members in class object are not,strictly speaking,members of any interface (§13.2). However,the members in class object are available via member lookup in any interface type (§7.4).
第7.4节:
- First,a set of accessible members named N is determined:
- …
- Otherwise,the set consists of all accessible (§3.5) members named N in T,including inherited members and the accessible members named N in object. […]
第7.4.1节:
For purposes of member lookup,a type T is considered to have the following base types:
…
• If T is an interface-type,the base types of T are the base interfaces of T and the class type object.
基本上它是一种软糖,让编译器理解对象的成员在执行时总是真的可用,即使它们实际上不是接口所涉及的表达式类型的成员.