c# - 执行时返回 null Type.GetType("System.Collections.Generic.SortedDictionary`2[System.String,System.String]");
问题描述
我正在尝试获取 SortedDictionary 的字符串化对象类型的类型,但它始终返回空值。但是,它适用于字典。
有用:
Type.GetType("System.Collections.Generic.Dictionary`2[System.String,System.String]");
不起作用并且总是返回一个空值:
Type.GetType("System.Collections.Generic.SortedDictionary`2[System.String,System.String]");
为什么以及如何解决这个问题?谢谢。
解决方案
仅Type.GetType(string)
当此类型位于当前正在执行的程序集中或位于mscorlib.dll
. 对于其他类型,需要assembly qualified name
指定。
Dictionary<TKey, TValue>
位于mscorlib.dll
(for .NET Framework
) 因此
Type.GetType("System.Collections.Generic.Dictionary`2[System.String,System.String]");
能够返回其类型。
SortedDictionary<TKey, TValue>
位于System.dll
因此
Type.GetType("System.Collections.Generic.SortedDictionary`2[System.String,System.String]");
返回null
。
要获取类型,SortedDictionary<TKey, TValue>
我们需要指定其assembly qualified name
:
Type.GetType(
"System.Collections.Generic.SortedDictionary`2[" +
"[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]," +
"[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]" +
", System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
这是演示它的演示。
推荐阅读
- javascript - 如何连接两个变量以记录另一个变量
- jquery - 资源(一些 css 和 js 文件)未在 Safari 浏览器(iphone 设备)中加载
- android - 从托管在 android Webview 中的网站获取用户登录详细信息
- java - 驱动程序无法通过使用安全套接字层在 java 1.8.0_161 上工作但不能在 1.8.0_202 上建立与 SQL Server 的安全连接
- windows - StringCbPrintf (strsafe.h) 是 WinAPI 的一部分吗?
- php - 从PHP到PostgreSQL查询很慢,怎么增加?
- sql-server - 如何删除具有所有 NULL 值的行
- angular - 如何在 cdk-virtual-scroll-viewport 内滚动时触发?
- javascript - 如何将 JSON 数据从 .js 文件转换为字符串
- matlab - 大型数据集,如何选择特定范围