単純な型階層の一部である別のインスタンスの型に基づいて、クラスのインスタンスをインスタンス化したいと考えています。
public abstract class Base
{
}
public class Derived1 : Base
{
}
public class Derived2 : Base
{
}
これは、次のコードで簡単に実行できます
Base d1 = new Derived1();
Base d2;
if (d1 is Derived1)
{
d2 = new Derived1();
}
else if (d1 is Derived2)
{
d2 = new Derived2();
}
ただし、if...else if...
(たとえば) リフレクションを使用して (私の例では) のコンストラクターを取得し、それを使用して、発生する可能性d1
のある型の別のインスタンスをインスタンス化することにより、チェーンなしでこれを達成することは可能ですか?d1