I have been trying to create an instance of a generic type from a config file, I knew that you should be able to do it but couldn’t figure out the syntax.  After flicking an email to our internal mailing list the following came back from the Mitch

using System;

using System.Collections.Generic;

using System.Text;

 

namespace GenericTypeStrings

{

    public class Program

    {

        public static void Main(string[] args)

        {

            Type listType = Type.GetType(“System.Collections.Generic.List`1[[System.Int32]]”);

            object listInstance = Activator.CreateInstance(listType);

            Type dictType = Type.GetType(“System.Collections.Generic.Dictionary`2[[System.Int32],[System.String]]”);

            object dictInstance = Activator.CreateInstance(dictType);

        }

    }

}

Take note of the List`1 syntax and the sqaure brackets which specific the type.   It is always so simple once you know how!

Advertisements