1 code
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Reflection; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace ConsoleApplication1 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 //要调用的函数所在的类是 person 15 Type tp = typeof(Person); 16 17 // 方法的名字是:HelloWorld 18 MethodInfo method = tp.GetMethod("HelloWorldSuper"); 19 20 //创造一个实例 21 object obj = Activator.CreateInstance(tp); 22 23 //调用函数 24 method.Invoke(obj, new object[] { 1,"2"}); 25 Console.ReadKey(); 26 } 27 } 28 29 public class Person 30 { 31 public void HelloWorld() 32 { 33 Console.WriteLine("HelloWorld"); 34 } 35 public void HelloWorldSuper(int a,string b) 36 { 37 Console.WriteLine("HelloWorld HelloWorld"); 38 } 39 } 40 }
2 show
时间: 2024-09-29 05:33:20