开发者

How to GetType of List<String> in C#? [duplicate]

开发者 https://www.devze.com 2023-01-07 20:19 出处:网络
This question already has answers here: Closed 12 years ago. How can I get the type of \"List<String>\" in C# using Type.GetType()?
This question already has answers here: Closed 12 years ago.

How can I get the type of "List<String>" in C# using Type.GetType()?

I have already tried:

 Type.GetType("List<String>");
 Type.GetType("System.Collections.Generic.List.String"); // Or [String]

Note that I can't use typeof since the va开发者_Python百科lue I am getting the type of is a string.


You can't get it from "List<String>", but you can get it from Type.GetType:

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

You're lucky in this case - both List<T> and string are in mscorlib, so we didn't have to specify the assemblies.

The `1 part specifies the arity of the type: that it has one type parameter. The bit in square brackets specifies the type arguments.

Where are you getting just List<String> from? Can you change your requirements? It's going to be easier than parsing the string, working out where the type parameters are, finding types in different namespaces and assemblies, etc. If you're working in a limited context (e.g. you only need to support a known set of types) it may be easiest to hard-code some of the details.


You can use typeof operator.

typeof(List<String>)

See this question: C# How can I get Type from a string representation


System.Type type = typeof(List<String>);


typeof(List<string>)
0

精彩评论

暂无评论...
验证码 换一张
取 消