• Type.MakeGenericType 方法 (Type[]) 泛型反射


    https://www.cnblogs.com/rinack/p/5680058.html

    替代由当前泛型类型定义的类型参数组成的类型数组的元素,并返回表示结果构造类型的 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
     */
  • 相关阅读:
    关于++i和i++的左值、右值问题
    运算符优先级
    计算机中的数及其编码
    递归函数
    PHP读取excel(4)
    替换元素节点replaceChild()
    子结点childNodes
    插入节点insertBefore()
    创建节点createElement
    插入节点appendChild()
  • 原文地址:https://www.cnblogs.com/nafio/p/11536205.html
Copyright © 2020-2023  润新知