简体   繁体   中英

Can not get Type of LinkedList`T with Type.GetType(string)

I am able to get type information of List<T> , Dictinary<TK, TVal> etc. by:

Type.GetType("System.Collections.Generic.List`1[System.String]")
Type.GetType("System.Collections.Generic.Dictionary`2[System.String,System.String[]]")

But failing to get type information of LinkedList<T> :

Type.GetType("System.Collections.Generic.LinkedList`1[System.String]") // Returns NULL
Type.GetType("System.Collections.Generic.LinkedList`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") // Also returns NULL

Inspecting the assembly of LinkedList<string>

typeof(LinkedList<decimal>).Assembly

Gives:

System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

Using .NET Standart 2.0.3.

Try to specify the fully qualified assembly name for System.Collections.Generic.LinkedList 1` type

var type = Type.GetType("System.Collections.Generic.LinkedList`1[System.String], System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

or just use assembly name

type = Type.GetType("System.Collections.Generic.LinkedList`1[System.String], System");

List<T> is part of mscorlib.dll , whether LinkedList<T> is part of System.dll

You were almost right here

Type.GetType("System.Collections.Generic.LinkedList`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") // Also returns NULL

But you've specified the fully qualified name for generic type parameter, which is System.String type, not for LinkedList .

Apparently, that's because List<> is defined in mscorlib.dll that is automatically being probed, while LinkedList<> is defined in System.dll (from the GAC).

Hence, you have to explicitly denote the assembly name when dynamically loading it:

Type.GetType("System.Collections.Generic.LinkedList`1, System")

From the Documentation :

If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM