これは前の質問へのフォローアップです。その質問には間違いがありました (私が探しているより良い解決策ではなく、現在の解決策を実際に投稿しました)。
私は3つのクラス、、、を持ってParentClass
いClassA
ますClassB
。ClassA
とはどちらClassB
も のサブクラスですParentClass
。タイプのオブジェクトを作成するClassA
かClassB
、ある種の列挙を使用してタイプを識別し、オブジェクトキャストを親タイプとしてインスタンス化します。どうすればそれを動的に行うことができますか? 以下のコードと、 と書かれている部分を見てください//what do I put here?
。読んでくれてありがとう!
public enum ClassType { ClassA, ClassB };
public abstract class ParentClass
{
public static readonly Dictionary<ClassType, Type> Types =
new Dictionary<ClassType, Type>{
{ClassType.ClassA, typeof(ClassA) },
{ClassType.ClassB, typeof(ClassB) }
};
public ParentClass()
{
//....
}
public static ParentClass GetNewObjectOfType(ClassType type)
{
//What do I put here?
}
}
public class ClassA:ParentClass
{
//....
}
public class ClassB:ParentClass
{
//.......
}