どこかからクラス AAA のオブジェクトを取得しており、そのオブジェクトにさらに情報を追加したいと考えています。そこで、AAA から派生した新しいクラス BBB を作成しています。クラス BBB には、追加のフィールド ディクショナリがあります。このディクショナリは、クラス AAA オブジェクトと、ディクショナリのキーとして使用したい項目の配列を取得する派生クラス コンストラクターで生成されます。このディクショナリーの値は、クラス AAA のオブジェクトのフィールドの要素です。打撃のサンプルコードで同様のシナリオを作成しようとしました:
void Main(){
A obj = new A () ;
obj.prop1 = new int [] {5 ,10, 15} ;
obj.prop2 = "Hello" ;
obj.prop3 = "World" ;
// obj.Dump () ;
B obj2 = new B (new int [] {1,2,3}, obj) ;
// obj2.Dump () ;
}
// Define other methods and classes here
public class A {
public int [] prop1 ;
public string prop2 ;
public string prop3 ;
}
public class B : A {
public Dictionary <int, int> prop4 ;
public B (int [] keys, A a) {
prop4 = new Dictionary <int, int> () ;
if (keys.Length == a.prop1.Length ) {
for (int i = 0 ; i < keys.Length ; i++ ) {
prop4.Add (keys[i], a.prop1[i]) ;
}
// is there a way to obsolete below lines of code???
this.prop1 = a.prop1 ;
this.prop2 = a.prop2 ;
this.prop3 = a.prop3 ;
}
else {
throw new Exception ("something wrong") ;
}
}
}
派生クラスのコンストラクターで、プロパティを手動で入力していますが、そうしたくありません。それを行う別の方法はありますか。実際のクラスには 20 を超えるプロパティがあります。