c# - Type.GetType ("System.Net.WebException")返回 null

标签 c# reflection

我正在尝试获取对 System.Net.WebException 的类型引用通过执行以下操作从其名称中获取:

var t = Type.GetType("System.Net.WebException");

tnull我不明白为什么。我的项目是一个针对.NET 4.6.1 的类库。我引用的是System并尝试引用 System.NetSystem.Web没有任何运气。

以下代码编译并返回正确的类型:

var t = typeof(System.Net.WebException);

但我需要string版本在这里工作。我也尝试过这个:

var t = Type.GetType("System.Net.WebException, System");

返回null也是如此。

有什么想法吗?

最佳答案

根据docs Type.GetType 中,如果类型位于 mscorlib.dll 中,则只能传递命名空间限定的类型名称,否则必须使用程序集限定的名称。 WebException不在 mscorlib.dll 中,所以你至少需要:

Type.GetType("System.Net.WebException, System, Version=4.0.0.0")

通过检查 typeof(WebException).AssemblyQualifiedName,您可以看到类似于:

System.Net.WebException, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

关于c# - Type.GetType ("System.Net.WebException")返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59006035/

相关文章:

c# - GetExecutingAssembly() 用于不同程序集中的派生类

c# - 如何在 C# 的 System.Type 中返回 System.__COMObject 的类型

c# - 如何在 SQL 中检索给定 StoredProcedure 参数的 .NET 类型?

java - 当我知道该类时如何正确调用方法?

c# - 如何在不同的 .aspx 文件或制作函数库中重用相同的函数?

c# - 使用 Ajax Beginform 加载不同的局部 View

c# - 立即构建 Kafka producer 出现 "Disconnected while requesting ApiVersion"错误

c# - linq to excel 异常 IErrorInfo.GetDescription

C# .net Core - 获取磁盘上的文件大小 - 跨平台解决方案

c# - 在 .NET 2.0 中将对象转换为 IEnumerable