Can something like this be accomplished using C#?
public abstract class BaseClass
{
public abstract IInterface<T> CreateEditor() where T: the_actual_type_of_this_instance;
}
Example usage:
var instance = new DerivedClass();
var editor = instance.CreateEditor(); // IInterface<DerivedClass>
No, you can't do that - partly because it wouldn't make sense at compile time. Consider a slight change to your code:
BaseClass instance = new DerivedClass();
var editor = instance.CreateEditor();
What could the compiler infer the type of editor
to be? It only knows about BaseClass
, so it would have to return an IInterface<BaseClass>
. Depending on whether or not T
in IInterface<T>
is covariant, that could be valid for something which actually implemented IInterface<DerivedClass>
or it might not be.
You might want to make your base class generic instead:
public abstract class BaseClass<T> where T : BaseClass<T>
{
public abstract IInterface<T> CreateEditor();
}
public class DerivedClass : BaseClass<DerivedClass>
{
...
}
There are two problems with this:
DerivedClass
, you'll have issuespublic class NastyClass : BaseClass<DerivedClass>
... but in many cases it's quite practical.
See more on this question at Stackoverflow