<分区>
我正在尝试使用 Type.GetType 并传递“caLibClient.entity.Web2ImageEntity”完整类名。 caLibClient.entity 是命名空间,位于单独的程序集 (caLibClient) 中并添加到程序引用程序集列表中。当我从程序中调用 Type.GetType 时,它总是返回 Null,有什么问题吗?
<分区>
我正在尝试使用 Type.GetType 并传递“caLibClient.entity.Web2ImageEntity”完整类名。 caLibClient.entity 是命名空间,位于单独的程序集 (caLibClient) 中并添加到程序引用程序集列表中。当我从程序中调用 Type.GetType 时,它总是返回 Null,有什么问题吗?
最佳答案
您还需要添加程序集名称,因为您的类型不在执行 程序集中(也不在 mscorlib 中。)因此调用应该是:
var myType = Type.GetType("caLibClient.entity.Web2ImageEntity, FullAssemblyName");
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.
来自docs for AssemblyQualifiedName
,这是一个示例名称:
TopNamespace.SubNameSpace.ContainingClass+NestedClass, MyAssembly, Version=1.3.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089
更新:如果您已经在您的项目中引用程序集,并且在编译时知道类型名称是什么,您最好说
Type myType = typeof(caLibClient.entity.Web2ImageEntity);
从现在起你不需要在运行时搜索类型;编译器会为你做一切。
关于c# - Type.GetType 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7283768/