I create an instance dynamically using Activator.CreateInstance. However, he says that the object cannot be null with every attempt. Paste the code below. Am I doing something wrong?
Is there a problem if
Activator.CreateInstance
replaces the usual switch / case statements to determine the type of an object at run time? Thank.
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)
{
BaseClass report =
(BaseClass) Activator.CreateInstance(Type.GetType("Derived"));
report.Func();
}
source
share