usingSystem;usingSystem.Reflection;namespaceconsoleapp2{classProgram {Static voidMain (string[] args) { //Reflection Get Namespace + class name stringClassName ="Consoleapp2.classsample"; stringMethodName ="test1"; //Passing ParametersObject[] Paras =NewObject[] {"my own","Computer" }; vart =Type.GetType (className); Objectobj =activator.createinstance (t); Try { #regionMethod One//Call directlyMethodInfo method = T.getmethod ("test2"); Method. Invoke (obj, paras); #endregion #regionMethod Twomethodinfo[] Info=T.getmethods (); for(inti =0; I < info. Length; i++) { varMD =Info[i]; //Method Name stringMothodname =Md. Name; //Parameter Collectionparameterinfo[] Paraminfos =Md. GetParameters (); //method name is the same as the number of arguments if(Mothodname = = MethodName && Paraminfos.length = =paras. Length) {Md. Invoke (obj, paras); } } #endregion } Catch(Exception ex) {Throwex; } console.readkey (); } } classClasssample { Public voidTest1 (stringpara1) {Console.WriteLine ("Mode 1 {0}________test111", PARA1); } Public voidTest1 (stringPARA1,stringpara2) {Console.WriteLine ("mode 2 {0}________test111________{1}", PARA1, PARA2); } Public voidTest2 (stringPARA1,stringpara2) {Console.WriteLine ("mode 3 {0}________test222________{1}", PARA1, PARA2); } }}
C # Gets the number of methods and parameters in the class by reflection, and the reflection calls the method with parameters