是否有更简单的方法来调用反射方法,而不是按照下面的方法创建methodInfo和object数组?
Assembly asm = Assembly.Load("Test");
Type t= asm.GetType("test.myclass");
object obj = Activator.CreateInstance(t);
MethodInfo mi = t.GetMethod("foo");
object[] args = { 10, 70 };
Console.WriteLine("output {0}", mi.Invoke(obj, args));
答案 0 :(得分:4)
使用动态关键字:
Assembly asm = Assembly.Load("Test");
Type t = asm.GetType("test.myclass");
dynamic obj = Activator.CreateInstance(t);
Console.WriteLine("output {0}", obj.Foo(10, 70));