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

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

  1. public virtual Type MakeGenericType(
  2. params Type[] typeArguments
  3. )

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

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

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

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

  1. using System;
  2. using System.Reflection;
  3. using System.Collections.Generic;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. Console.WriteLine("\r\n--- Create a constructed type from the generic Dictionary type.");
  10.  
  11. // Create a type object representing the generic Dictionary
  12. // type, by omitting the type arguments (but keeping the
  13. // comma that separates them, so the compiler can infer the
  14. // number of type parameters).
  15. Type generic = typeof(Dictionary<,>);
  16. DisplayTypeInfo(generic);
  17.  
  18. // Create an array of types to substitute for the type
  19. // parameters of Dictionary. The key is of type string, and
  20. // the type to be contained in the Dictionary is Test.
  21. Type[] typeArgs = { typeof(string), typeof(Test) };
  22.  
  23. // Create a Type object representing the constructed generic
  24. // type.
  25. Type constructed = generic.MakeGenericType(typeArgs);
  26. DisplayTypeInfo(constructed);
  27.  
  28. // Compare the type objects obtained above to type objects
  29. // obtained using typeof() and GetGenericTypeDefinition().
  30. Console.WriteLine("\r\n--- Compare types obtained by different methods:");
  31.  
  32. Type t = typeof(Dictionary<String, Test>);
  33. Console.WriteLine("\tAre the constructed types equal? {0}", t == constructed);
  34. Console.WriteLine("\tAre the generic types equal? {0}",
  35. t.GetGenericTypeDefinition() == generic);
  36. }
  37.  
  38. private static void DisplayTypeInfo(Type t)
  39. {
  40. Console.WriteLine("\r\n{0}", t);
  41.  
  42. Console.WriteLine("\tIs this a generic type definition? {0}",
  43. t.IsGenericTypeDefinition);
  44.  
  45. Console.WriteLine("\tIs it a generic type? {0}",
  46. t.IsGenericType);
  47.  
  48. Type[] typeArguments = t.GetGenericArguments();
  49. Console.WriteLine("\tList type arguments ({0}):", typeArguments.Length);
  50. foreach (Type tParam in typeArguments)
  51. {
  52. Console.WriteLine("\t\t{0}", tParam);
  53. }
  54. }
  55. }
  56.  
  57. /* This example produces the following output:
  58.  
  59. --- Create a constructed type from the generic Dictionary type.
  60.  
  61. System.Collections.Generic.Dictionary`2[TKey,TValue]
  62. Is this a generic type definition? True
  63. Is it a generic type? True
  64. List type arguments (2):
  65. TKey
  66. TValue
  67.  
  68. System.Collections.Generic.Dictionary`2[System.String, Test]
  69. Is this a generic type definition? False
  70. Is it a generic type? True
  71. List type arguments (2):
  72. System.String
  73. Test
  74.  
  75. --- Compare types obtained by different methods:
  76. Are the constructed types equal? True
  77. Are the generic types equal? True
  78. */

Type.MakeGenericType 方法 (Type[]) 泛型反射的更多相关文章

  1. 使用Type.MakeGenericType,反射构造泛型类型

    有时我们会有通过反射来动态构造泛型类型的需求,该如何实现呢?举个栗子,比如我们常常定义的泛型委托Func<in T, out TResult>,当T或TResult的类型需要根据程序上下文 ...

  2. Activator.CreateInstance 方法 (Type) 的用法

    转自:http://www.cnblogs.com/lmfeng/archive/2012/01/30/2331666.html Activator.CreateInstance 方法 (Type) ...

  3. Swift编程语言学习12 ——实例方法(Instance Methods)和类型方法(Type Methods)

    方法是与某些特定类型相关联的函数.类.结构体.枚举都能够定义实例方法:实例方法为给定类型的实例封装了详细的任务与功能.类.结构体.枚举也能够定义类型方法:类型方法与类型本身相关联.类型方法与 Obje ...

  4. 3.java的hello word.继承.泛型.反射.配置项.数据库操作.lombok

    迷迷茫茫的开始了第一步.弄个hello word.结果这第一小步也不是那么的顺利. 明明照着图敲的.可就是没有运行选项. 为此还百度了一下.也没有什么答案.最后只能老老实实的看了.结果还是粗心的问题. ...

  5. C#工具:反射帮助类 泛型反射帮助类

    反射帮助类 using System; using System.Reflection; using System.Data; using System.Drawing; using System.R ...

  6. java 反射和泛型-反射来获取泛型信息

    通过指定对应的Class对象,程序可以获得该类里面所有的Field,不管该Field使用private 方法public.获得Field对象后都可以使用getType()来获取其类型. Class&l ...

  7. [C#] 解决Silverlight反射安全关键(SecuritySafeCritical)时报“System.MethodAccessException: 安全透明方法 XXX 无法使用反射访问”的问题

    作者: zyl910 一.缘由 在Silverlight中使用反射动态访问时,经常遇到"System.MethodAccessException: 安全透明方法 XXX 无法使用反射访问-- ...

  8. Java的泛型反射

    If the superclass is a parameterized type, the {@code Type} * object returned must accurately reflec ...

  9. swift 中Value Type VS Class Type

    ios 中Value Type 和 Class Type 有哪些异同点,这个问题是在微信的公共帐号中看到的,觉得挺有意思,这里梳理一下. 1.swift 中为什么要设置值类型? 值类型在参数传递.赋值 ...

随机推荐

  1. php小程序生成二维码

    <?php getwxacode(); //生成二维码 function getwxacode(){ $url = "https://api.weixin.qq.com/wxa/get ...

  2. vue-cli设置引入目录

    打开build/webpack.base.conf.js 找到module.exports下的resolve这行 刚开始是这样的 resolve: { extensions: ['.js', '.vu ...

  3. 查看CPU位数的方法

                                                                查看电脑cpu的位数   WINDOWS下查看的 方法: 方法一. 在开始→运行 ...

  4. python实现策略模式

    python实现策略模式 原文地址 1.策略模式概述 策略模式:定义一系列算法,把它们一一封装起来,并且使它们之间可以相互替换.此模式让算法的变化不会影响到使用算法的客户. 电商领域有个使用“策略”模 ...

  5. AIX中逻辑卷管理

    1.逻辑卷管理 逻辑卷的大小确定: 逻辑卷大小(MB)=PP的大小(MB)*LV包含的LP的个数 LV占用的物理空间(MB)=PP的大小(MB)*LV包含的LP的个数*LV拷贝的副本数   逻辑卷控制 ...

  6. linux 读取文本的最后几行

    tail -n 100 filename    读取file的最后100行 tail: n. 尾巴:踪迹:辫子:燕尾服 vt. 尾随:装上尾巴 vi. 跟踪:变少或缩小 adj. 从后面而来的:尾部的 ...

  7. jenkins 构建时显示git分支插件、显示构建分支插件

    参数化构建分支 1.安装插件:Git Parameter 2.找到我们在Jenkins中建立的工程,勾选“参数化构建过程”,并如下配置 3.在“源码管理”中如下配置 Jenkins构建完显示构建用户和 ...

  8. cnblogs博客使用LaTeX公式

    $ Entropy\ H(X) = -\sum p(X)\log p(X) $ $ Information\ Gain\ I(X,Y)= H(X)-H(X|Y) $ $ \pi $ = 3.14159 ...

  9. WPF 样式Style

    一:样式基础 如果我们的程序有三个这样的按键,一般我们会这样写 <StackPanel> <!--按键的背景色为Azure蔚蓝色背景色为Coral珊瑚色字体为Arial加粗字体大小为 ...

  10. Docker(2)--Centos7 上安装部署

    Centos7 上安装docker Docker从1.13版本之后采用时间线的方式作为版本号,分为社区版CE和企业版EE. 社区版是免费提供给个人开发者和小型团体使用的,企业版会提供额外的收费服务,比 ...