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("HelloWorld"); 19 20 //创造一个实例 21 object obj = Activator.CreateInstance(tp); 22 23 //调用函数 24 method.Invoke(obj, null); 25 Console.ReadKey(); 26 } 27 } 28 29 public class Person 30 { 31 public void HelloWorld() 32 { 33 Console.WriteLine("HelloWorld"); 34 } 35 } 36 }
2 show
时间: 2024-12-28 20:16:06