c# - Type.GetType 返回 null

标签 c# gettype

<分区>

我正在尝试使用 Type.GetType 并传递“caLibClient.entity.Web2ImageEntity”完整类名。 caLibClient.entity 是命名空间,位于单独的程序集 (caLibClient) 中并添加到程序引用程序集列表中。当我从程序中调用 Type.GetType 时,它​​总是返回 Null,有什么问题吗?

最佳答案

您还需要添加程序集名称,因为您的类型不在执行 程序集中(也不在 mscorlib 中。)因此调用应该是:

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

来自Type.GetType() docs :

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/

相关文章:

c# - 如何使用 DataContractSerializer 仅序列化/反序列化基类属性?

c# - 在静态方法中调用非静态 GetType()?

c# - 在 Blazor 中增加 TextArea

C# 反射 : How to get the type of a Nullable<int>?

c# - Powershell:GetType() 打印所有类型信息,但缺少很多?

c# - 可空类型不是可空类型?

c# - DataTable.Load() 未从 ExecuteReader() 获取数据

c# - 具有 String.Format 功能的日志记录方法

c# - 使用 .NET Core 的 DI 容器实例化对象