Type: MakeGenericType : Generic Type « Reflection « C# / CSharp Tutorial






using System;
using System.Collections.Generic;

public class MainClass
{
    static void Main() {
        IList<int> intList = (IList<int>) CreateClosedType<int>( typeof(List<>) );

        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 );
    }
}
System.Collections.Generic.List`1[System.Int32]
System.Collections.Generic.List`1[System.Double]








19.10.Generic Type
19.10.1.Type: MakeGenericType
19.10.2.Reflection with Generics
19.10.3.Using Reflection with Generic Types