Method taking a type name as argument used against a collection of type names

181 views Asked by At

When using the operator typeof we provide a type name to obtain a System.Type instance:

System.Type t = typeof(System.Int32)

or simply:

Type t = typeof(int)

What is the exact name/nature of the parameter passed to the operator?.

How can I build a collection of such "type names":

???[] typeNames = { System.Int32, System.String };
foreach (??? tn in typeNames) aMethod(tn);
1

There are 1 answers

3
Jon Skeet On BEST ANSWER

What is the exact name/nature of the parameter passed to the operator?.

A type name - not as a string, but literally present in the code.

How can I build a collection of such "type names"

You can't, but you could use typeof multiple times:

Type[] types = { typeof(int), typeof(string) };

If you're actually interested in the names, you can use the nameof operator:

string[] names = { nameof(Int32), nameof(String) };