public void GetProps(Parent p){
// want to access lots of child properties here
string childProp1 = p.prop1;
bool childProp2 = p.prop2;
bool childProp3 = p.prop3;
}
但是编译器抱怨说
“父级不包含定义 prop1”
该函数将采用父类的不同子类型。
所有的子类都有这个
public override string prop1 { get; set; }
有没有办法做到这一点?
编辑: 使问题更清楚
我目前有一个巨大的 if-elseif 我做类似的事情
if(p is Child0){
Child0 ch = p as Child0;
// want to access lots of child properties here
string childProp1 = ch.prop1;
bool childProp2 = ch.prop2;
bool childProp3 = ch.prop3;
}else if(p is Child1){
Child1 ch = p as Child1;
// want to access lots of child properties here
string childProp1 = ch.prop1;
bool childProp2 = ch.prop2;
bool childProp3 = ch.prop3;
}else if(...// and many more
现在我想删除所有冗余代码并创建一个可以处理所有这些的函数。