.NET CORE动态调用泛型方法详解
本文实例为大家分享了.NETCORE动态调用泛型方法,供大家参考,具体内容如下
usingSystem;
usingSystem.Reflection;
namespaceDynamicCall
{
classProgram
{
staticvoidMain(string[]args)
{
Console.WriteLine("HelloWorld!");
Programp=newProgram();
varti=p.GetType().GetTypeInfo();
varmtd=ti.GetMethod("Get");
Console.WriteLine(mtd?.ToString()??"nogetmethod.");
vargenMethod=mtd.MakeGenericMethod(typeof(int));
varobj=genMethod.Invoke(p,newobject[]{});
Console.WriteLine(obj?.ToString()??"nogetresult.");
Console.ReadLine();
}
publicstringGet()
{
returntypeof(T).FullName;
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。