Dynamic Method Invocation using Reflection - With Parameters and Return Value

In this code snippet, you will be knowing how to create an object and invoke the method dynamically.
Class which we are going to instantiate and invoke a method in that dynamically.

public class ReflectionClass
public int AddNumbers(int number1, int number2)
return number1 + number2;

To invoke the method, put the following piece of code. Make you have imported or written using for System.Reflection.

Type ReflectionObject = typeof(ReflectionClass);
object classObject = Activator.CreateInstance(ReflectionObject);
ReflectionObject.GetMethod("AddNumbers").Invoke(classObject, new object[] {3,4 });


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: