码迷,mamicode.com
首页 > Web开发 > 详细

.NET CORE 动态调用泛型方法

时间:2017-08-24 22:38:22      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:cti   var   color   obj   console   调用   泛型方法   result   ati   

 

 1 using System;
 2 using System.Reflection;
 3 
 4 namespace DynamicCall
 5 {
 6     class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             Console.WriteLine("Hello World!");
11             Program p = new Program();
12             var ti = p.GetType().GetTypeInfo();
13             var mtd = ti.GetMethod("Get");
14 
15             Console.WriteLine(mtd?.ToString() ?? "no get method.");
16 
17             var genMethod = mtd.MakeGenericMethod(typeof(int));
18 
19             var obj = genMethod.Invoke(p, new object[] { });
20 
21             Console.WriteLine(obj?.ToString() ?? "no get result.");
22 
23             Console.ReadLine();
24         }
25 
26         public string Get<T>()
27         {
28             return typeof(T).FullName;
29         }
30     }
31 }

 

.NET CORE 动态调用泛型方法

标签:cti   var   color   obj   console   调用   泛型方法   result   ati   

原文地址:http://www.cnblogs.com/ybst/p/7425357.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!