我正在尝试获取对 System.Net.WebException
的类型引用通过执行以下操作从其名称中获取:
var t = Type.GetType("System.Net.WebException");
t
是 null
我不明白为什么。我的项目是一个针对.NET 4.6.1 的类库。我引用的是System
并尝试引用 System.Net
和System.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/