简体   繁体   中英

How to call reflection method

Is there an easier way to call reflection method instead of create methodInfo and object array as per below?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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