当前位置:主页 > 查看内容

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

发布时间:2021-07-01 00:00| 位朋友查看

简介:本文实例为大家分享了.NET CORE动态调用泛型方法,供大家参考,具体内容如下 using System;using System.Reflection;namespace DynamicCall{ class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); Program p = new Progra……

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

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;
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持站长技术。


原文链接:https://m.jb51.net/article/122067.htm
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐