问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Reflection;namespace复习{classProgram{staticvoidMain(string[]args){Typet1=typeof(Person);MethodInfomi1=t1.GetMethod("SayHello",BindingFlags.NonPublic|BindingFlags.Instance,null,newType[]{},null);MethodInfomi2=t1.GetMethod("SayHello",BindingFlags.NonPublic|BindingFlags.Instance,null,newType[]{typeof(String)},null);MethodInfomi3=t1.GetMethod("SayHello",BindingFlags.NonPublic|BindingFlags.Instance,null,newType[]{typeof(String),typeof(Int32)},null);//如何分别反射以下三个方法?//privatevoidSayHello<T1,T2,T3>(T1t1,T2t2,T3t3)//privatevoidSayHello<T1>(T1t)//privatevoidGM1<T1>(T1t)objectobj1=Activator.CreateInstance(t1);//开始调用//Console.ReadKey();}}classPerson{privatevoidSayHello(){Console.WriteLine("我是一个无参的方法");}privatevoidSayHello(strings){Console.WriteLine("我是一个有参的方法,参数:{0}",s);}privatevoidSayHello(strings,inti){Console.WriteLine("我是一个有两个参数的方法,参数:{0}、{1}",s,i);}privatevoidSayHello<T1,T2,T3>(T1t1,T2t2,T3t3){Console.WriteLine(t1.ToString()+t2.ToString()+t3.ToString());}privatevoidSayHello<T1>(T1t){Console.WriteLine(t.ToString());}privatevoidGM1<T1>(T1t){Console.WriteLine(t.ToString());}}}
解决方案
解决方案二:
解决方案三:
以下是调用PM1的示例:Personp=newPerson();Typet=p.GetType();MethodInfomi=t.GetMethod("GM1",BindingFlags.NonPublic|BindingFlags.Instance);MethodInfomi2=mi.MakeGenericMethod(newType[]{typeof(string)});mi2.Invoke(p,newobject[]{"abcdefg"});
解决方案四:
参考:
解决方案五:
markmark
解决方案六:
标个记号回头来看
解决方案七:
C#类成员分Field、Property和Method三种,Field通过名称就可以直接通过反射获取,后两种需要加上参数类型才能唯一确定,以Method为例,不同方法名称相同而参数不同是允许的(重载),但反射时就比较麻烦了。classProgram{staticvoidMain(string[]args){Typet1=typeof(Person);MethodInfomi1=GetMethod(t1,"SayHello");MethodInfomi2=GetMethod(t1,"SayHello",typeof(string));MethodInfomi3=GetMethod(t1,"SayHello",typeof(int));//如何分别反射以下三个方法?//privatevoidSayHello<T1,T2,T3>(T1t1,T2t2,T3t3)MethodInfomi4=GetGenericMethod(t1,"SayHello",typeof(string),typeof(string),typeof(int));//privatevoidSayHello<T1>(T1t)MethodInfomi5=GetGenericMethod(t1,"SayHello",typeof(string));//privatevoidGM1<T1>(T1t)MethodInfomi6=GetGenericMethod(t1,"GM1",typeof(string));}staticMethodInfoGetMethod(Typetype,stringname,paramsType[]types){returntype.GetMethod(name,BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance,null,types,null);}staticMethodInfoGetGenericMethod(Typetype,stringname,paramsType[]types){foreach(MethodInfomiintype.GetMethods(BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance)){if(mi.Name!=name)continue;if(!mi.IsGenericMethod)continue;if(mi.GetGenericArguments().Length!=types.Length)continue;returnmi.MakeGenericMethod(types);}thrownewMissingMethodException();}}classPerson{privatevoidSayHello(){Console.WriteLine("我是一个无参的方法");}privatevoidSayHello(strings){Console.WriteLine("我是一个有参的方法,参数:{0}",s);}privatevoidSayHello(strings,inti){Console.WriteLine("我是一个有两个参数的方法,参数:{0}、{1}",s,i);}privatevoidSayHello<T1,T2,T3>(T1t1,T2t2,T3t3){Console.WriteLine(t1.ToString()+t2.ToString()+t3.ToString());}privatevoidSayHello<T1>(T1t){Console.WriteLine(t.ToString());}privatevoidGM1<T1>(T1t){Console.WriteLine(t.ToString());}}
这里没考虑泛型方法带非泛型参数的情况,如果需要可自己琢磨。
解决方案八:
也许可以利用dynamic的语法糖?dynamicd=obj1asdynamic;d.SayHello<int,double,string>(0,1.0,"str");