为什么不使用类似的东西(例如):
public class Base
{
public virtual Base Parent { get {return ...}}
public virtual BaseConnection Connection { get {return ... ;}}
}
public class Special : Base
{
public override Base Parent { get {return MySpecialParent;}}
public override BaseConnection Connection { get {return new SpecialConnection();}}
}
甚至类似的东西(如果 BaseConnection 和 SpecialConnection 之间没有关系):
public class Base
{
public virtual dynamic Parent { get {return ...}}
public virtual dynamic Connection { get {return ... ;}}
}
public class Special : Base
{
public override dynamic Parent { get {return MySpecialParent;}}
public override dynamic Connection { get {return new SpecialConnection();}}
}
但是也许你应该再考虑一下你的类分离,也许你应该从基类的创建者那里注入特定的关系,或者使用某种容器并使用接口来代替。只是一个想法 ... :)