不能完全弄清楚基地/派生List<type>
的动态。主项目“派生”引用“基础”项目。基地项目工作正常,MOD需要保持在最低限度。派生项目仅添加与服务的交互,但不会修改基本功能本身。基类名单<type>财产往/从派生类名单<derivedType>财产
基类:
B1
{
private int _prop;
int prop
{
get{return _prop;}
set{_prop = value;}
}
}
B2
{
private List<B1> _list;
List<B1> list
{
get{return _list;}
set{_list = value;}
}
}
派生类:
D1:B1
{
int prop
{
get{return base.prop;}
set{base.prop = value;}
}
}
D2:B2
{
List<D1> list
{
get{return base.list;}/Problem area ... each base.B1 needs to be cast to a D1.
set{base.list = value;}//... each D1 needs to be cast back to a B1
}
}
在base.list每个B1必须转换为D1,以使用D1的具体方法,其又与相互作用外部服务。已经尝试过,但我不确定这是一个更好的策略。 (存在重载的构造函数以支持此操作。)
get
{
List<D1> listD1 = new List<D1>();
foreach (B1 b in base.list)
{
listD1.Add(new D1(B1));
}
return _listD1;
}
set
{
List<B1> listB1 = new List<B1>();
foreach (D1 d in list)
{
listB1.Add(new B1(d.prop));
}
base.list = listB1;
}
是否有更优雅的方式?
非常有帮助......谢谢。是的,这是一种特殊情况,其中由于派生的静态方法,基本属性必须可以派生为派生属性。因此,派生的属性现在有独特的名称,一切都很好。再次,谢谢。 –