Invoke public methods by using their names in C#

invoke public methods

You can use reflection to invoke public methods by using their names. The following code shows how the example program does this.

// Invoke the method.
private void btnInvoke_Click(object sender, EventArgs e)
        Type this_type = this.GetType();
        MethodInfo method_info =
        method_info.Invoke(this, null);
    catch (Exception ex)

The code uses GetType to get the class’s type information. It then calls GetMethod, passing it the method’s name, to get a MethodInfo object describing the method.

Next it calls that object’s Invoke method to invoke the method. The second parameter to this call is an array of parameters that should be sent to the method call.

Reflection lets you do lots of other things with objects such as learn about and invoke their properties and events.

Download Example   Follow me on Twitter   RSS feed

This entry was posted in classes, reflection and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.