Ok, this arises from some discussion going on here.
Imagine the following scenario. Company Alpha publishes library Charlie which exposes among others, a type Charlie.Bar that explicilty implements the interface IFooable:
public interface IFooable
{
void Foo();
}
namespace Charlie
{
public clas Bar: IFooable
{
void IFooable.Foo() {...}
....
}
}
Now it happens that company Beta is subclassing Charlie.Bar in its library Tango for some very particular functionality where the explicit implementation of IFooable.Foo doesn't cut it. Beta needs to "override" the interface implementation and achieve a seemingly equivalent virtual behavior; Calls to IFooable.Foo() should resolve correctly to the runtime type of the object. Note that having company Alpha modify the implementation of Charlie.Bar is not a viable option.
So how can this be done? Explicit interface methods are marked in CIL as virtual and final so you can not override them. Beta came up with this "hack":
using Charlie;
namespace Tango
{
class Blah: Bar, IFooable
{
void IFooable.Foo() { //Bar specific implementation }
}
}
Note the implementation of IFooable again in Blah even though it is redundant. With this code, you actually get the same behavior as a virtual method when calling IFooable.Foo():
IFooable fooBar = new Bar();
IFooable fooBlah = new Blah();
fooBlah.Foo() // Blah's implementation is called.
fooBar.Foo() // Bar's implementation is called.
Is this a very ugly hack? (I've seen it done in similarly justified? scenarios). What are the alternatives if any?