.NET CORE动态调用泛型方法详解

时间:2022-09-26 07:38:53

本文实例为大家分享了.NET CORE动态调用泛型方法,供大家参考,具体内容如下

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

 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/ybst/archive/2017/08/24/7425357.html