James P. Wright
James P. Wright

Reputation: 9131

Cannot seem to use GetType with Activator.CreateInstance

I am trying to get an object using Reflection, and then launch a method on that object. I was getting null from Type.GetType("my.namespace.item") so I decided to try a test that SHOULD work. Using this code Type.GetType((new my.namespace.item()).GetType().FullName) I still get null.

That should not happen from what I understand. What am I doing wrong?

Upvotes: 0

Views: 1224

Answers (1)

dlev
dlev

Reputation: 48606

You're only specifying the FullName of the Type, which is (ironically) not the full name you need. Type.GetType(string) requires the AssemblyQualifiedName of the Type in order to work:

Type.GetType((new my.namespace.item()).GetType().AssemblyQualifiedName)

should be fine. Specifying it manually would look like:

Type.GetType("Namespace.TypeName, MyAssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089");

Obviously you can omit the Version, Culture, or PublicKeyToken if they don't apply.

Upvotes: 1

Related Questions