简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM