Zugriff auf untergeordnete Eigenschaften über die übergeordnete Referenz?
<code>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; } </code>
Der Compiler beklagt sich jedoch darüber
"Parent enthält keine Definition prop1"
Die Funktion würde verschiedene Untertypen der Klasse Parent annehmen.
Alle Unterklassen haben dies
<code>public override string prop1 { get; set; } </code>
Gibt es eine Möglichkeit, dies zu erreichen?
BEARBEITEN: Um die Frage klarer zu machen
Ich habe derzeit ein riesiges if-elseif, in dem ich so etwas mache
<code>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 </code>
Jetzt wollte ich den gesamten redundanten Code entfernen und eine Funktion erstellen, die all dies bewältigen kann.