[英]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. 我试图使用Reflection获得一个对象,然后在该对象上启动一个方法。 I was getting null from Type.GetType("my.namespace.item")
so I decided to try a test that SHOULD work. 我从Type.GetType("my.namespace.item")
中获取了空值,所以我决定尝试进行一项应能工作的测试。 Using this code Type.GetType((new my.namespace.item()).GetType().FullName)
I still get null. 使用此代码Type.GetType((new my.namespace.item()).GetType().FullName)
我仍然得到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
的FullName
,(讽刺地)不是所需的全名。 Type.GetType(string)
requires the AssemblyQualifiedName
of the Type
in order to work: Type.GetType(string)
需要Type
的AssemblyQualifiedName
才能起作用:
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. 显然,如果它们不适用,则可以省略Version,Culture或PublicKeyToken。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.