
IList<double> doubleList =
(IList<double>)
CreateClosedType<double>( typeof(List<>) );
Console.WriteLine( intList );
Console.WriteLine( doubleList );
}
static object CreateClosedType<T>( Type genericType ) {
Type[] typeArguments = {
typeof( T )
};
Type closedType =
genericType.MakeGenericType( typeArguments );
return Activator.CreateInstance( closedType );
}
}
The meat of this code is inside the generic method CreateClosedType<T>. All of the work is
done in general terms via references to Type created from the available metadata. First, you need to
get a reference to the generic, open type List<>, which is passed in as a parameter. After that, you
simply create ...