.NET CORE动态调用泛型方法详解
来源:动视网
责编:小OO
时间:2020-11-27 22:35:24
.NET CORE动态调用泛型方法详解
本文实例为大家分享了.NET CORE动态调用泛型方法,供大家参考,具体内容如下:using System;using System.Reflection;namespace DynamicCall{ class Program { static void Main(string[] args) { Console.WriteLine("Hello World
导读本文实例为大家分享了.NET CORE动态调用泛型方法,供大家参考,具体内容如下:using System;using System.Reflection;namespace DynamicCall{ class Program { static void Main(string[] args) { Console.WriteLine("Hello World

本文实例为大家分享了.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;
}
}
}
.NET CORE动态调用泛型方法详解
本文实例为大家分享了.NET CORE动态调用泛型方法,供大家参考,具体内容如下:using System;using System.Reflection;namespace DynamicCall{ class Program { static void Main(string[] args) { Console.WriteLine("Hello World