I need to create a generic type, but I do not know the type at compile time. I would like to do this:
Type t = typeof(whatever);
var list = new List
Like this:
Type t;
Type genericListType = typeof(List<>).MakeGenericType(t);
object list = Activator.CreateInstance(genericListType);
Note that you can only assign it to a variable of type object
. (Although you can cast to to the non-generic IList
interface)
To use the list
variable, you'll probably need reflection.