Dynamic Method Invocation using Reflection - Without Parameters

Hi,
This code snippets shows you how you can create an object for a class at runtime and make a call to the member method.


public class ReflectionClass
{
public void GetName()
{
Console.WriteLine("Brainstorming Guy");
}
}

We are going to make a call to GetName method using Reflection. Make user you have included System.Reflection.

Type ReflectionObject = typeof(ReflectionClass);
object classObject = Activator.CreateInstance(ReflectionObject);
ReflectionObject.GetMethod("GetName").Invoke(classObject, null);

This will invoke the method GetName in the class ReflectionClass.


Comments

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:
    Email: