using System; using System.Reflection; namespace AssemblyTest { class Program { static void Main(string[] args) { //利用反射创建对象 Assembly asm = Assembly.GetExecutingAssembly(); Object obj = asm.CreateInstance("AssemblyTest.TestClass", true); //调用对象的成员方法 Type t = typeof(TestClass); t.InvokeMember("Fun", BindingFlags.InvokeMethod, null, obj, null); //调用静态方法 t.InvokeMember("StaticFun", BindingFlags.InvokeMethod, null, null, null); Console.ReadLine(); } } class TestClass { public void Fun() { Console.WriteLine("Invoke Fun"); } public static void StaticFun() { Console.WriteLine("Invoke Static Fun"); } } }
运行效果