Well, this is because the discussion is here.
Imagine the following scenario. Alpha publishes the Charlie library, which provides, among others, the Charlie.Bar type, which explicilty implements the IFooable interface:
public interface IFooable { void Foo(); } namespace Charlie { public clas Bar: IFooable { void IFooable.Foo() {...} .... } }
Now, it happens that a beta version of a company subclasses Charlie.Bar in its Tango library for some special functionality, where the explicit implementation of IFooable.Foo does not shorten it. Bets need to "override" the implementation of the interface and achieve apparently equivalent virtual behavior; The calls to IFooable.Foo() should correctly resolve the runtime type of the object. Please note that having Alpha to modify the Charlie.Bar implementation Charlie.Bar not a viable option.
How can I do that? Explicit interface methods are marked in virtual and final in CIL, so you cannot override them. Beta came up with this hack:
using Charlie; namespace Tango { class Blah: Bar, IFooable { void IFooable.Foo() {
Note the implementation of IFooable again in Blah , even if it is redundant. With this code, you actually get the same behavior as the virtual method when calling IFooable.Foo() :
IFooable fooBar = new Bar(); IFooable fooBlah = new Blah(); fooBlah.Foo() // Blah implementation is called. fooBar.Foo() // Bar implementation is called.
Is this a very ugly hack? (I saw this in similarly justified scenarios). What are the alternatives, if any?
source share