C#: Type.GetType returns null for System.Collections.Generic



我在运行时会发出

Type.GetType(
    "System.Collections.Generic.Stack`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]")

我会在运行时得到预期的类型

Type.GetType(
    "System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]")

堆栈类型有什么问题?在这种情况下,为什么getType返回null?

您应该使用type.semblyqualifiedname而不是Type.FullName

Type.GetType("System.Collections.Generic.Stack`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

这正确返回了 typeof(Stack<Int32>)

的等效物

这考虑到堆栈不与INT和列表(MSCORLIB)相同的组件中,而是在系统中,这就是为什么它在没有适当的组装资格的情况下返回null的原因。

最新更新