C# instantiate generic List from reflected Type [duplicate]

Try this:

void foobar(Type t)
{
    var listType = typeof(List<>);
    var constructedListType = listType.MakeGenericType(t);

    var instance = Activator.CreateInstance(constructedListType);
}

Now what to do with instance? Since you don’t know the type of your list’s contents, probably the best thing you could do would be to cast instance as an IList so that you could have something other than just an object:

// Now you have a list - it isn't strongly typed but at least you
// can work with it and use it to some degree.
var instance = (IList)Activator.CreateInstance(constructedListType);

Leave a Comment