It's impossible.
In my opinion, I do not think that the shell should be implemented using inheritance.
For example, suppose we have an Engine class, and you need to implement FerrariEngine . And you have a Car class.
You say that Car should inherit FerrariEngine . It looks awful for me!
At the end of the day, you want to do something like dependency injection using inheritance, and again, this is not the right way.
My suggestion is do not try to make your life easier: decide architecture based on rational points.
UPDATE
OP said in some comment:
I want this class to manage instances of objects of type T, so that the client does not need to care when instances are needed to create.
You do not need to do weird things to get what you want:
public interface IEngine { void Start(); } public sealed class FerrariEngine : IEngine { public FerrariEngine() { Start(); } public void Start() { } } public abstract class Car<TEngine> where TEngine: IEngine, new() { public Car() { _engine = new Lazy<TEngine>(() => new TEngine()); } private readonly Lazy<TEngine> _engine; public TEngine Engine { get { return _engine.Value; } } } public class FerrariCar : Car<FerrariEngine> { }
Finally, if we create an instance of FerrariCar :
Car<FerrariEngine> myFerrari = new FerrariCar();
The engine will be created and started without developer intervention!
Check how Lazy<T> and the main general restrictions are fulfilled by the task;)
In short:
- Using
Lazy<T> , the mechanism will be created only with some access to the Engine property. - When the created lazy engine starts, because
FerrariEngine implements a parameterless constructor that calls Start() itself, it will start the engine.
I believe this example shows you how you can get what you are looking for and use C # "as is"!
MatΓas Fidemraizer
source share