有没有办法将“this”传递给基本构造函数?
显然,在
我也没有看到在
abstract class Base<TSelf>
{
public ICollection<TSelf> List;
public Base(ICollection<TSelf> list, TSelf self)
{
List = list;
List.Add(self);
}
}
class Implementation : Base<Implementation>
{
public Implementation() : base(new List<Implementation>(), this)
{
}
}
显然,在
Implementation
的构造函数中,将this
传递给base
存在编译错误。我也没有看到在
Base
级别实例化list
的任何方法。
this
强制转换为TSelf
之前将其转换为object
是解决方案! - Manuel Fauxwhere T : Base
来强制执行吗? - Manuel FauxTSelf selfThis = (TSelf) (object) this;
改为TSelf selfThis = this as TSelf;
,在将TSelf
约束为class
后会有什么不利影响吗? - Bill Tür stands with Ukraine