简体   繁体   English

如何调用反射法

[英]How to call reflection method

Is there an easier way to call reflection method instead of create methodInfo and object array as per below? 有没有一种更简单的方法来调用反射方法,而不是按照下面的方法来创建methodInfo和对象数组?

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));

Use dynamic keyword: 使用动态关键字:

Assembly asm = Assembly.Load("Test");
Type t = asm.GetType("test.myclass");
dynamic obj = Activator.CreateInstance(t);
Console.WriteLine("output {0}", obj.Foo(10, 70));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM