HOW TO CALL REFLECTION METHOD

ben reitman

Total Post:96

Points:676
Posted by  ben reitman
C# 
C#
 1306  View(s)
Ratings:
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));

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: How to call reflection method

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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!