zoukankan      html  css  js  c++  java
  • Type.MakeGenericType 方法 (Type[]) 泛型反射

    替代由当前泛型类型定义的类型参数组成的类型数组的元素,并返回表示结果构造类型的 Type 对象。

    命名空间:   System
    程序集:  mscorlib(mscorlib.dll 中)

    public virtual Type MakeGenericType(
        params Type[] typeArguments
    )

    参数
    typeArguments
    将代替当前泛型类型的类型参数的类型数组。

    返回值
    Type: System.Type
    Type 表示的构造类型通过以下方式形成:用 typeArguments 的元素取代当前泛型类型的类型参数。

    备注
    MakeGenericType 方法,您可以编写将特定类型分配给泛型类型定义,从而创建的类型参数的代码 Type 表示特定的构造的类型的对象。
    您可以使用此 Type 对象来创建构造类型的运行时实例。

    示例
    下面的示例使用 MakeGenericType 方法来创建一个构造的类型的泛型类型定义从 Dictionary<TKey, TValue> 类型。
    构造的类型表示 Dictionary<TKey, TValue> 的 Test 字符串的键的对象。

    using System;
    using System.Reflection;
    using System.Collections.Generic;
    
    public class Test
    {
        public static void Main()
        {
            Console.WriteLine("
    --- 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("
    --- Compare types obtained by different methods:");
    
            Type t = typeof(Dictionary<String, Test>);
            Console.WriteLine("	Are the constructed types equal? {0}", t == constructed);
            Console.WriteLine("	Are the generic types equal? {0}", 
                t.GetGenericTypeDefinition() == generic);
        }
    
        private static void DisplayTypeInfo(Type t)
        {
            Console.WriteLine("
    {0}", t);
    
            Console.WriteLine("	Is this a generic type definition? {0}", 
                t.IsGenericTypeDefinition);
    
            Console.WriteLine("	Is it a generic type? {0}", 
                t.IsGenericType);
    
            Type[] typeArguments = t.GetGenericArguments();
            Console.WriteLine("	List type arguments ({0}):", typeArguments.Length);
            foreach (Type tParam in typeArguments)
            {
                Console.WriteLine("		{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
     */
  • 相关阅读:
    P3373 【模板】线段树 2
    P3372 【模板】线段树 1
    P3368 【模板】树状数组 2
    P3374 【模板】树状数组 1
    P1004 方格取数
    P1880 [NOI1995]石子合并
    UOJ#152盘子序列
    P1886 滑动窗口
    P1440 求m区间内的最小值
    二进制中1的个数
  • 原文地址:https://www.cnblogs.com/rinack/p/5680058.html
Copyright © 2011-2022 走看看