namespace ReflectionTest { public class test { public void writeString(string s) { Console.WriteLine("writeString:" + s); } public static void staticWrite(string s) { Console.WriteLine("staticString:" + s); } public string addString(string s) { return s + s; } } }
类
调用 记得调用 using System.Reflection;
static void Main(string[] args) { Assembly dll = Assembly.LoadFile(@"C:UsersFoxDesktop反射学习ReflectionTestinDebugReflectionTest.dll"); Type type = dll.GetType("ReflectionTest.test"); Console.WriteLine(type.FullName); MethodInfo mi = type.GetMethod("writeString"); object obj = dll.CreateInstance("ReflectionTest.test"); Object[] oo = new Object[] { "wq" }; //调用非静态方法 第一个参数得为其创建一个实例 mi.Invoke(obj, oo); mi = type.GetMethod("staticWrite"); //调用静态方法 第一个参数可以为NULL mi.Invoke(null, oo); Console.ReadLine(); }