.NET CORE 动态调用泛型方法

时间:2017-08-24 22:27:30   收藏:0   阅读:371

 

 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 }

 

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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!