如果一个类IFoo.Bar
显式实现,而派生类需要IFoo.Bar
做一些不同的事情,那么派生类将无法调用该方法的基类实现。不重新实现的派生类IFoo.Bar
可以通过 调用基类实现((IFoo)this).Bar()
,但是如果派生类重新实现IFoo.Bar
(因为它必须为了改变其行为),上述调用将转到派生类 re -实现,而不是基类实现。即使((IFoo)(BaseType)this).bar
没有帮助,因为将引用转换为接口类型将丢弃有关被转换的引用类型(与实例实例的类型相反)的任何信息。
Having an explicit interface implementation do nothing but call a protected method avoids this problem, since a derived class can change the behavior of the interface method by overriding the virtual method, while retaining the ability to call the base implementation as it sees fit. IMHO, C# should have had an explicit interface implementation produce a virtual method with a CLS-compliant name, so someone writing in C# a derivative of a class that explicitly implemented IFoo.Bar
could say override void IFoo.Bar
, and someone writing in some other language could say, e.g. Overrides Sub Explicit_IFoo_Bar()
; since any derived class can re-implement IFoo.Bar
, and since any derived class which doesn't re-implement IFoo.Bar
can call it on itself, I don't see that there's any useful purpose to having the explicit implementation be sealed.
Incidentally, in vb.net, the normal pattern would simply be Protected Overridable Sub IFoo_Bar() Implements IFoo.Bar
, without need for a separate virtual method.