logeeks
logeeks

Reputation: 4979

Activator.CreateInstance failing

I have am creating an Instance dynamically using Activator.CreateInstance. However, it is saying object cannot be null on every attempt. Pasting the code below. Am I doing anything wrong?

Is there any problem if

Activator.CreateInstance

replaces the conventional switch/case statements for determining the object type in the run-time? Thanks.

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();
}

Upvotes: 5

Views: 5878

Answers (5)

Guillaume Slashy
Guillaume Slashy

Reputation: 3624

At Runtime, your GetType call will return null. You have to :

  • either Precise your namespace
  • or Use typeof

Upvotes: 0

dknaack
dknaack

Reputation: 60438

Type.GetType("Derived" cant find the type

Simeple change

BaseClass report = (BaseClass) Activator.CreateInstance(Type.GetType("Derived"));

to this

Base report = (Base)Activator.CreateInstance(typeof(Derived));

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1499770

Well, Type.GetType("Derived") is almost certainly returning null - which would make it nothing to do with Activator.CreateInstance.

Check:

  • Is Derived in the same assembly as the calling code? If not, use Assembly.GetType on the right assembly, or include the assembly name in the type name you're passing to Type.GetType()
  • Is your type in a namespace? If so, you need to namespace-qualify it

Upvotes: 3

Fredrik Mörk
Fredrik Mörk

Reputation: 158289

From the documentation of the typeName parameter of Type.GetType:

The assembly-qualified name of the type to get. See AssemblyQualifiedName. If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

This means that you need to (at least) pass the namespace as well:

BaseClass report = (BaseClass) Activator.CreateInstance(Type.GetType("YourNamespace.Derived")); 

Upvotes: 3

George Duckett
George Duckett

Reputation: 32418

The GetType method is failing and returning null. See the parameters section of the previous link.

The assembly-qualified name of the type to get. See AssemblyQualifiedName. If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

Add the namespace before "Derived" and if the Derived class is in a different assembly, then add ", assemblyname" to the end.


Note that if you aren't going to vary the string you pass into GetType, then you could just use typeof(Derived) (although in that case there isn't much point using Activator.CreateInstance).

Upvotes: 1

Related Questions