static void Main(string[] args) { GenericMethod(); LogInfo(); } static void GenericMethod() { MethodInfo mi = typeof(Program).GetMethod("Echo"); Console.WriteLine(mi.IsGenericMethodDefinition); Console.WriteLine(mi.Invoke(null, new object[] { 123 })); } public static T Echo<T>(T x) { return x; }
To remidy this issue,we must specify concrect types of generic methods.
static void Main(string[] args) { GenericMethod(); LogInfo(); } static void GenericMethod() { MethodInfo mi = typeof(Program).GetMethod("Echo"); MethodInfo intMi = mi.MakeGenericMethod(typeof(int)); Console.WriteLine(intMi.IsGenericMethodDefinition); Console.WriteLine(intMi.Invoke(null, new object[] { 123 })); } public static T Echo<T>(T x) { return x; }
The key located at specify concrete type
//MakeGenericMethod MethodInfo intMi = mi.MakeGenericMethod(typeof(int));
标签:operations,Memberinfo,methods,mi,static,WriteLine,MethodInfo,Console,GenericMeth From: https://www.cnblogs.com/Fred1987/p/18062529