Home > DeveloperSection > Forums > How to call reflection method
ben reitman
ben reitman

Total Post:96

Posted on    February-09-2014 11:28 PM

 C# C# 

 1 Reply(s)
 1278  View(s)
Rate this:

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

Pravesh Singh

Total Post:412

Posted on    February-09-2014 11:50 PM

Hi Ben,

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

Don't want to miss updates? Please click the below button!

Follow MindStick