scatman scatman - 1 year ago 163
C# Question

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

Possible Duplicate:

How can I get generic Type from a string representation?

How can I get the type of
in C# using

I have already tried:

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

Note that I can't use
since the value I am getting the type of is a string.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download