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;
}
但是编译器抱怨C#,从父引用访问子属性?
“父不包含定义为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
现在我想删除所有的冗余代码,使一个巨大的函数可以处理所有这些。
我已经回答最好,我可以给粗略的信息 - 如果你可以给你更多的上下文关于你想达到什么,那真的会有所帮助。 – 2012-04-29 08:09:41
你如何使用关键字ref作为变量参数? – 2012-04-29 08:12:12
@DarrenDavies编辑问题 – 2012-04-29 08:16:21