I ran into a problem today trying to override an implementation of an interface method which had not been declared virtual.
In this case I'm not able to change the interface or the base implementation and have to try something else, but I was wondering if there was a way to force a class to implement an interface using virtual methods.
Example:
interface IBuilder<T>
{
// Already implicitly virtual
/*virtual*/ T Build();
}
// This is a class written by someone else
class SimpleBuilder: IBuilder<SomeObject>
{
// I would like to force this to be virtual
public SomeObject Build() { return new SomeObject(); }
}
// This is the class I am writing.
class CustomBuilder: SimpleBuilder
{
public /*override*/ SomeObject Build()
{
var obj = base.Build();
obj.ModifyInSomeWay();
return obj;
}
}
Edit: CustomBuilder
is intended to be used with MEF so I am deriving from SimpleBuilder
in order for MEF to resolve the correct type. I've tried explicitly implementing the interface and not deriving from SimpleBuilder
at all but then MEF doesn't pick up the right type.
The interface and base class in question are in a shared module created by another developer so it looks like I will have to get them to change their base class anyway.