C#:房产压倒一切通过指定接口明确 [英] C#: Property overriding by specifying the interface explicitly

查看:151
本文介绍了C#:房产压倒一切通过指定接口明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在试图重写的ICollection<的显式接口实现; T> .IsReadOnly 物业的收藏< T> 类,我碰到一些文件,说明显式接口成员实现不能被覆盖,因为他们不能有修饰语,如虚拟来到摘要。在 MSDN 的,他们甚至去尽可能指定如何做出明确接口通过创建另一个则是由显式接口成员实现调用抽象的或虚拟的成员可以继承成员的实现。 。没有问题至今

While attempting to override the explicit interface implementation of the ICollection<T>.IsReadOnly property from the Collection<T> class, I came across some documents stating that explicit interface member implementations cannot be overridden because they cannot have modifiers such as virtual or abstract. On MSDN they even go as far as specifying how to make an explicit interface member implementation available for inheritance by creating another abstract or virtual member which is called by the explicit interface member implementation. No problems so far.

但后来我想:为什么有可能在C#中覆盖的任何的显式实现接口成员只是指定接口的明确

But then I wonder: Why is it possible in C# to override any explicitly implemented interface member just by specifying the interface explicitly?

例如,假设我有一个简单的界面,这样,具有属性和方法:<? / p>

For example, suppose I have a simple interface like this, with a property and method:

public interface IMyInterface
{
    bool AlwaysFalse { get; }
    bool IsTrue(bool value);
}

和类 A 其中明确实现的接口,并有一个方法测试()这就要求自己的接口成员实现。

And a class A which implements the interface explicitly, and has a method Test() which calls its own interface member implementation.

public class A : IMyInterface
{
    bool IMyInterface.AlwaysFalse
    { get { return false; } }

    bool IMyInterface.IsTrue(bool value)
    { return value; }

    public bool Test()
    { return ((IMyInterface)this).AlwaysFalse; }
}



正如你所看到的,没有一个四位成员都是虚拟的或抽象的,所以当我定义一个类 b 是这样的:

public class B : A
{
    public bool AlwaysFalse
    { get { return true; } }

    public bool IsTrue(bool value)
    { return !value; }
}



然后,你会期望的一个实例b 转换为 A 来表现得像 A 。它所做的:

Then you'd expect an instance of B cast to A to behave like A. And it does:

A a = new A();
Console.WriteLine(((IMyInterface)a).AlwaysFalse);    // False
Console.WriteLine(((IMyInterface)a).IsTrue(false));  // False
Console.WriteLine(a.Test());                         // False
A b = new B();
Console.WriteLine(((IMyInterface)b).AlwaysFalse);    // False
Console.WriteLine(((IMyInterface)b).IsTrue(false));  // False
Console.WriteLine(b.Test());                         // False

现在来捕捞。创建一个类 C B 除了在类声明一件事的精确副本:

Now comes the catch. Create a class C which is an exact copy of B except for one thing in the class declaration:

public class C : A, IMyInterface
{ /* ... same as B ... */ }

现在的C实例,当转换为 A ,并没有表现得像 A 但像 C

Now an instance of C, when cast to A, doesn't behave like A but like C:

A c = new C();
Console.WriteLine(((IMyInterface)c).AlwaysFalse);    // True
Console.WriteLine(((IMyInterface)c).IsTrue(false));  // True
Console.WriteLine(c.Test());                         // True



甚至测试()方法现在调用重载方法 C !这是为什么

Even the Test() method now calls the overridden method in C! Why is this?

推荐答案

这具有的没有的做用显式接口实现?;它只是继承和接口映射的一般规则的后果:你会看到的究竟的相同的结果,如果键入 A 提供了一个隐含的,而不是明确,实施 IMyInterface的

This has nothing to do with explicit interface implementation; it's simply a consequence of the general rules of inheritance and interface mapping: you would see exactly the same results if type A provided an implicit, rather than explicit, implementation of IMyInterface.


  • 键入 b 从类型继承 A 。没有被覆盖。结果
    B 提供了自己的 AlwaysFalse IsTrue运算成员,但他们的的实施 IMyInterface的; IMyInterface的由成员提供的实现在继承 A :当类型的实例被转换为 IMyInterface的则完全相同的行为方式类型的实例 A ,因为 A 将提供实现该接口的成员。

  • 键入 C 从类型 A 继承。同样,没有被覆盖。结果
    C 提供了自己的 AlwaysFalse IsTrue运算成员,但此时这些成员的的实施 IMyInterface的:当类型的实例 ç被转换为 IMyInterface的然后 C 提供的接口实现,而不是那些成员的 A

  • Type B inherits from type A. Nothing is overridden.
    B provides its own AlwaysFalse and IsTrue members but they don't implement IMyInterface; The implementation of IMyInterface is provided by the members inherited from A: when an instance of type B is cast to IMyInterface then it behaves in exactly the same way as an instance of type A because A is providing the members that implement the interface.
  • Type C inherits from type A. Again, nothing is overridden.
    C provides its own AlwaysFalse and IsTrue members but this time those members do implement IMyInterface: when an instance of type C is cast to IMyInterface then the members of C provide the interface implementation rather than those of A.

由于类型 A 工具 IMyInterface的明确编译器不警告 B的成员和<$ C $ ç> C 都躲藏的 A 成员;实际上 A 的成员都已经隐藏由于显式接口实现。

Because type A implements IMyInterface explicitly the compiler doesn't warn that the members of B and C are hiding the members of A; In effect those members of A were already hidden due to the explicit interface implementation.

如果您更改类型 A 实施 IMyInterface的隐而不显式地那么编译器会警告说, B <成员/ code>和 C 都躲起来,不重写, A ,您应该理想地使用 修改声明中的成员时, b C

If you changed type A to implement IMyInterface implicitly rather than explicitly then the compiler would warn that the members of B and C are hiding, not overriding, the members of A and that you should ideally use the new modifier when declaring those members in B and C.

下面是一些相关的位从语言规范。 (在 ECMA-334规范第20.4.2和20.4.4; 13.4节1.4和在微软C#4规格13.4.6 。)

Here are some relevant bits from the language specification. (Sections 20.4.2 and 20.4.4 in the ECMA-334 spec; sections 13.4.4 and 13.4.6 in the Microsoft C#4 spec.)

20.4.2接口映射

特定
接口成员 IM ,其中 I 的实施是
该成员 M
声明的接口,是由
检查每个类或结构下定决心
开头 C 和重复为
每一个连续的基类的C
,直到比赛的位置。

The implementation of a particular interface member I.M, where I is the interface in which the member M is declared, is determined by examining each class or struct S, starting with C and repeating for each successive base class of C, until a match is located.

20.4.4接口重新执行

这是继承接口
实现的类允许
重新执行的由
接口,包括它在基类列表。一个
重新实现一个接口
的遵循完全相同的接口
映射规则作为初始
实施的接口。因此,
继承的接口映射对为
重新实现的接口建立了接口
映射没有
任何作用。

A class that inherits an interface implementation is permitted to re-implement the interface by including it in the base class list. A re-implementation of an interface follows exactly the same interface mapping rules as an initial implementation of an interface. Thus, the inherited interface mapping has no effect whatsoever on the interface mapping established for the re-implementation of the interface.

这篇关于C#:房产压倒一切通过指定接口明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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