Type.GetType返回null [英] Type.GetType return null

查看:230
本文介绍了Type.GetType返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Type.GetType并传递"caLibClient.entity.Web2ImageEntity"完整的类名. caLibClient.entity是命名空间,位于单独的程序集(caLibClient)中,并已添加到程序引用程序集列表中.当我从程序中调用Type.GetType时,它总是返回Null,这是怎么回事?

I am trying to use Type.GetType and pass "caLibClient.entity.Web2ImageEntity" full class name. The caLibClient.entity is namespace, located in separated assembly (caLibClient) and added to program reference assemblies list. The Type.GetType always return Null when I call it from program, what is wrong?

推荐答案

您还需要添加程序集名称,因为您的类型不在 executing 程序集(也不是mscorlib)中.呼叫应该是:

You need to add the assembly name as well, since your type isn't in the executing assembly (nor mscorlib.) So the call should be:

var myType = Type.GetType("caLibClient.entity.Web2ImageEntity, FullAssemblyName");

Type.GetType()文档:

typeName
类型:System.String
要获取的类型的程序集限定名称.看 AssemblyQualifiedName.如果类型在当前执行中 程序集或Mscorlib.dll中,提供类型名称就足够了 通过其名称空间来限定.

typeName
Type: System.String
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.

AssemblyQualifiedName文档中,这是一个示例名称:

From the docs for AssemblyQualifiedName, this is a sample name:

TopNamespace.SubNameSpace.ContainingClass+NestedClass, MyAssembly, Version=1.3.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089

更新:如果您已经在项目中引用了程序集,并且在编译时知道了类型名,那么最好说一下

Update: If you're already referencing the assembly in your project, and know at compile-time what the type-name is, you're better off saying

Type myType = typeof(caLibClient.entity.Web2ImageEntity);

因为现在您不需要在运行时搜索类型;编译器将为您做所有事情.

since now you don't need to search for the type at run-time; the compiler will do everything for you.

这篇关于Type.GetType返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆