Encoding closed generic type names as string literals.

A question came across one of our internal mailing lists today about how to encode closed generic type names as string literals for placement in things like configuration files. This is actually possible, here is an example for a generic list of integers:


Notice the ‘1 notation? That signifies the number of type parameters that follow, so if I had a generic type that took two type parameters (for example a Dictionary<K,V>), then it would be encoded as follows:


In general the best way to find out how to encode type names is to write a quick little console application and get the runtime to tell you the correct notation, for example:

Console.WriteLine(new Dictionary().GetType().FullName);

Once you have the notation right you can instansiate the type as follows:

Type listType = Type.GetType(“System.Collections.Generic.List‘1[[System.Int32]]”);
object listInstance = Activator.CreateInstance(listType);

Easy peasy!


3 thoughts on “Encoding closed generic type names as string literals.

  1. Kashif Azam

    Thnx for the solution … but how can we iterate through all the items in the list say we have created a list from activator.createinstance and that we have retrieved in object
    how can we write a loop for each item on it

    Please help,
    Thnx in advanaced

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s