

.NET 2.0+

Invoking Overloaded Methods Using Reflection

Static and instance methods can be invoked via reflection by obtaining a MethodInfo object for the desired member and calling the Type class's Invoke method. When invoking overloaded methods, the call to obtain the method information must be modified.

Invoking Methods

During the Reflection tutorial I described the basic ways in which you can reflect the methods of a type and invoke those methods. Using the techniques described in those earlier articles you can call the methods of late-bound objects. You can even call non-public methods, though this should be a last resort, as those methods may be unexpectedly refactored during updates, making them unavailable.

The basic process that you follow in order to invoke a method using reflection has several steps. Firstly you must obtain a Type object to represent the class or structure containing the method you wish to execute. Next, you can call GetMethod on the Type object to obtain details of the method, held in a MethodInfo object. Finally, you use the Type class's Invoke method to execute the code. If the method being called has parameters, you pass values to these via an array of objects.

Invoking Overloaded Methods

If the method that you wish to invoke is overloaded, the manner of obtaining a MethodInfo instance must be modified. Let's consider an example class, shown below:

public class Test
    public void ShowMessage()
        Console.WriteLine("Hello, world");

    public void ShowMessage(string msg, int timesToShow)
        for (int i = 0; i < timesToShow; i++)

If the Test class contained only one method with the name, "ShowMessage", we could obtain this method's information as described in the earlier articles. We would use the following code:

MethodInfo method = type.GetMethod("ShowMessage");

With our test class, the above call is invalid. If you try to run it, the program will throw an AmbiguousMatchException, indicating that there is more than method with that name. To avoid this problem you need to specify the types of the parameters of the target method, as well as its name. We don't need to specify the return type for the method, as it's illegal in C# to create overloads whose signatures differ only by their return types.

To provide the desired parameter types, a second argument is added to the call to GetMethod. This contains an array of Type instances. The first item in the array must correspond to the first parameter of the desired method with subsequent array elements matching the remaining parameters in the order in which they appear.

For example, try adding the following code to the Main method of the program. The third line uses reflection to find a method that has the name, "ShowMessage", and that includes two parameters. The parameters are, in order, a string and an integer. As such a method does exist in the Test class, a valid MethodInfo is returned and the Invoke method executes it successfully.

Test test = new Test();
Type type = test.GetType();
MethodInfo method = type.GetMethod("ShowMessage"
    , new Type[] { typeof(string), typeof(int) });
method.Invoke(test, new object[] { "Exterminate!", 3 });




If you wish to find the overload with no parameters, you simply need to provide an empty Type array to GetMethod's second parameter:

MethodInfo method = type.GetMethod("ShowMessage", new Type[0]);
method.Invoke(test, new object[0]);
2 June 2013