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