执行 Type.GetType( "System.Collections.Generic.SortedDictionary`2[System.String,System.String]" 时返回 nu



我试图获取SortedDictionary的字符串化对象类型的类型,但它总是返回null值。但是,它可以与Dictionary一起使用。

它的作用:Type.GetType("System.Collections.Generic.Dictionary`2[System.String,System.String]");

不起作用,并且总是返回null值:Type.GetType("System.Collections.Generic.SortedDictionary`2[System.String,System.String]");

为什么以及如何解决此问题?谢谢

只有当类型位于当前执行程序集中或mscorlib.dll中时,方法Type.GetType(string)才能按名称返回类型。对于其他类型,它需要指定assembly qualified name

Dictionary<TKey, TValue>位于mscorlib.dll中(对于.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]");

返回CCD_ 11。

要获得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");

这是演示它。

相关内容

最新更新