Activator.CreateInstanceを使用してインスタンスを動的に作成しています。しかし、それはオブジェクトがすべての試みでnullになることはできないと言っています。以下のコードを貼り付けます。私は間違って何かしていますか?Activator.CreateInstance failed
Activator.CreateInstance
は、実行時にオブジェクトタイプを決定するための従来のスイッチ/ case文を置き換えた場合に何か問題はありますか?ありがとう。
public abstract class Base
{
public abstract void Func();
}
public class Derived:Base
{
public override void Func()
{
MessageBox.Show("Derived First");
}
}
public class Derived2 : Base
{
public override void Func()
{
MessageBox.Show("Derived Second");
}
}
private void button1_Click(object sender, EventArgs e)
{
// I was trying to make use of the overladed version
// where it takes the Type as parameter.
BaseClass report =
(BaseClass) Activator.CreateInstance(Type.GetType("Derived"));
report.Func();
}
を使用して正確ないずれかの代わりにType.GetTypeの??:あなたがする必要はあり –