xiaolizi007
3/13/2017 - 5:57 AM

泛型类型 转换 Type.MakeGenericType Method

泛型类型 Type.MakeGenericType #csharp

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {

            //IsGenericType
            ///////////////////////////////////////
            var v1 = typeof (DateTime).IsGenericType;
            Console.WriteLine(v1);  //false
            var v2 = typeof (List<int>).IsGenericType;
            Console.WriteLine(v2);  //true
            var v3 = typeof (Dictionary<,>).IsGenericType;
            Console.WriteLine(v3);  //true

            //T[], List[] 等等数组时, IsGenericType为False
            //typeof(T[]).GetElementType().IsGenericType才是True
            var v4 = typeof(List<int>[]).IsGenericType;
            Console.WriteLine(v4);   //false


            //IsGenericTypeDefinition
            //MakeGenericType
            //////////////////////////////
            //获取一个值,该值指示当前 Type 是否表示可以用来构造其他泛型类型的泛型类型定义。 
            var v5 =typeof (List<int>).IsGenericTypeDefinition;
            Console.WriteLine(  v5);//false
            var v6 = typeof (List<>).IsGenericTypeDefinition;
            Console.WriteLine(  v6);//true
            //例子
            var typeList = typeof(List<>);
            Type typeDataList = typeList.MakeGenericType(typeof(DateTime)); //通过List<>构建出List<DateTime>

            //IsGenericParameter
            //////////////////////////////
            //表明当前类型是一个T类型 
            var t = typeof (List<int>);
            t = typeof(Dictionary<string,object>);
            if (t.IsGenericType)
            {
                // If this is a generic type, display the type arguments.
                //
                Type[] typeArguments = t.GetGenericArguments();

                Console.WriteLine("\tList type arguments ({0}):",
                    typeArguments.Length);

                foreach (Type tParam in typeArguments)
                {
                    // If this is a type parameter, display its
                    // position.
                    //
                    if (tParam.IsGenericParameter)
                    {
                        Console.WriteLine("\t\t{0}\t(unassigned - parameter position {1})",
                            tParam,
                            tParam.GenericParameterPosition);
                    }
                    else
                    {
                        Console.WriteLine("\t\t{0}", tParam);
                    }
                }
            }
        }
    }
}
using System;
using System.Reflection;
using System.Collections.Generic;

public class Test
{
    public static void Main()
    {
        Console.WriteLine("\r\n--- Create a constructed type from the generic Dictionary type.");

        // Create a type object representing the generic Dictionary 
        // type, by omitting the type arguments (but keeping the 
        // comma that separates them, so the compiler can infer the
        // number of type parameters).      
        Type generic = typeof(Dictionary<,>);
        DisplayTypeInfo(generic);

        // Create an array of types to substitute for the type
        // parameters of Dictionary. The key is of type string, and
        // the type to be contained in the Dictionary is Test.
        Type[] typeArgs = { typeof(string), typeof(Test) };

        // Create a Type object representing the constructed generic
        // type.
        Type constructed = generic.MakeGenericType(typeArgs);
        DisplayTypeInfo(constructed);

        // Compare the type objects obtained above to type objects
        // obtained using typeof() and GetGenericTypeDefinition().
        Console.WriteLine("\r\n--- Compare types obtained by different methods:");

        Type t = typeof(Dictionary<String, Test>);
        Console.WriteLine("\tAre the constructed types equal? {0}", t == constructed);
        Console.WriteLine("\tAre the generic types equal? {0}", 
            t.GetGenericTypeDefinition() == generic);
    }

    private static void DisplayTypeInfo(Type t)
    {
        Console.WriteLine("\r\n{0}", t);

        Console.WriteLine("\tIs this a generic type definition? {0}", 
            t.IsGenericTypeDefinition);

        Console.WriteLine("\tIs it a generic type? {0}", 
            t.IsGenericType);

        Type[] typeArguments = t.GetGenericArguments();
        Console.WriteLine("\tList type arguments ({0}):", typeArguments.Length);
        foreach (Type tParam in typeArguments)
        {
            Console.WriteLine("\t\t{0}", tParam);
        }
    }
}

/* This example produces the following output:

--- Create a constructed type from the generic Dictionary type.

System.Collections.Generic.Dictionary`2[TKey,TValue]
        Is this a generic type definition? True
        Is it a generic type? True
        List type arguments (2):
                TKey
                TValue

System.Collections.Generic.Dictionary`2[System.String, Test]
        Is this a generic type definition? False
        Is it a generic type? True
        List type arguments (2):
                System.String
                Test

--- Compare types obtained by different methods:
        Are the constructed types equal? True
        Are the generic types equal? True
 */
if (t.IsGenericType)
{
    // If this is a generic type, display the type arguments.
    //
    Type[] typeArguments = t.GetGenericArguments();

    Console.WriteLine("\tList type arguments ({0}):", 
        typeArguments.Length);

    foreach (Type tParam in typeArguments)
    {
        // If this is a type parameter, display its
        // position.
        //
        if (tParam.IsGenericParameter)
        {
            Console.WriteLine("\t\t{0}\t(unassigned - parameter position {1})",
                tParam,
                tParam.GenericParameterPosition);
        }
        else
        {
            Console.WriteLine("\t\t{0}", tParam);
        }
    }
}