.NET CORE 动态调用泛型方法

 

 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 }

 

posted @ 2017-08-24 21:31  乁卬杨  阅读(2410)  评论(0编辑  收藏  举报