The situation is as follows. You want to call a method in the currently executing namespace, but you do not know the name of the static method while coding (perhaps the method name is dynamic and saved in a database or text file). How do you dynamically call the static method with just a string name that is only available at runtime? All it takes is FIVE lines of code to accomplish this with the System.Reflection namespace!
Assume the following situation that is depicted in the full example at the bottom of this tutorial:
- Namespace is MyNamespace
- Class name is LateBinding
- Three public static Method (Method1, Method2, Method3) that require zero parameters
STEP ONE – To state the current Assembly: Since the method we want to call is contained within the same codebehind file, it is in what is called the Executing Assembly.
Assembly assemblyInstance = Assembly.GetExecutingAssembly();
STEP TWO – To state the Type:Â To state the type, we need the Namespace and the Class name. In our example, the namespace is called MyNamespace and the Class is called LateBinding. That leads to the below snippet:
Type thisClass = assemblyInstance.GetType("MyNamespace.LateBinding", false, true);
- The first argument is the conbination of the namespace and class name as a string.
- The second argument is a boolean variable where true will throw an exception of the first argument is false. If the variable is set to false, it will return null.
- The third argument is another boolean variable will true ignores the case of the first argument and false make the first argument case sensitive.
STEP THREE – To state the Method: From the Type we obtained from the previous step, we will store information regarding our method in a MethodInfo object. In this proof of concept example, we will call Method1. At this point, we don’t care about the number of parameters.
MethodInfo m = thisClass.GetMethod("Method1");
STEP FOUR – To call the Method: Now that we have a MethodInfo object, we can call the method via Invoke. Invoke has several different overloads, but in this example, we will use the most simple one with two arguments. It returns an object which we’ll call result. The result stores the return value of the method we are trying to call.Â
object result = m.Invoke(null, null);
- This first parameter is looking for an object, but because our methods are all static, we can safely leave this argument as null.
- The second parameter is an object array of arguments. Since our methods all take zero parameters, this is also null. If you’re method requires a string, you would need to add the string input to the object array.
STEP FIVE – To get the result: Now we know the output of our method is a string, so we’ll convert the result (which is of type Object) to a string.
string s = result.ToString();
We have now called the method via a string. This entire process is called Late Binding and can be used to extend your applications in several ways. One method as stated in the early portion of this tutorial is trying to call a method that has been stored as a name in the database.
Finally, as promised, below is the codebehind with this example:
using System;
using System.Reflection;
namespace MyNamespace
{
public partial class LateBinding : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Assembly assemblyInstance = Assembly.GetExecutingAssembly();
Type thisClass = assemblyInstance.GetType("MyNamespace.LateBinding", false, true);
MethodInfo m = thisClass.GetMethod("Method1");
object result = m.Invoke(null, null);
string s = result.ToString();
}
public static string Method1()
{
return "Called Method 1";
}
public static string Method2()
{
return "Called Method 2";
}
public static string Method3()
{
return "Called Method 3";
}
}
}
RSS Feed
Posted in
Tags: 
