I'm looking for the best way to implement the following situation (.NET 3.5):
interface IGetThing<T>
{
T Get();
}
class BaseGetter<A> : IGetThing<A> where A : new()
{
public virtual A Get()
{
return new A();
}
}
class DerivedGetter<B, A> : Base, IGetThing<B> where B : A, new() where A : new()
{
public override A Get()
{
return Get(); //B version
}
public new virtual B Get()
{
return new B();
}
}
I've evaluated posts like This one, but I cannot see a solution that it would provide that is equivalent.
I've seen suggestions that I use explicit interface implementation to do something similar, but I don't see how that solves the inheritance issue:
If Get() was implemented explicitly in both places, it wouldn't solve the problem of: ((IGetThing<A>)new DerivedGetter<B, A>()).Get()
calling the base method, instead of the desired derived method.
Attempting to implement both IGetThing and IGetThing in DerivedGetter causes a compilation exception. ('DerivedGetter' cannot implement both 'IGetThing' and 'IGetThing' because they may unify for some type parameter substitutions)
Also, attempting to re-implement BaseGetter's explicit implementation (IGetThing<A>.Get()
) in DerivedGetter also provides a compilation exception (The obvious 'DerivedGetter.IGetThing<...>.Get()': containing type does not implement interface 'IGetThing')
The goal is to hide and override the base's Get() when using Derived.
Does anyone have any ideas?
EDIT: The overall solution would preferably be able to scale to multiple layers of derived classes.
As an aside, this only started giving me compilation issues when I changed from .NET 4 to .NET 3.5.