C# – Call methods by String names – a simple .net script parser

.Net library System.Reflection allows you to call any methods by its name as string. You can apply this method when you want to parse texts from a script file, such as GiveMeValue(100). “GiveMeValue(100)” is a string, and you want to call a real GiveMeValue(int input) method inside your code. With the help of System.Reflection, you can call any functions with any kind of parameters from string text.

The reason I need to call methods from string names is because I want to make a simple C# Script parser. With this script parser, my application can simply call any inside functions by reading commands from a raw text file (a self-made script file?).

Below is a method that will accept another method name and its parameters as string. Parameters, however, you can make them as a params object[] fore more flexibility. Please note that this method call another method within its class, and the called method is not static.

1
2
3
4
5
6
7
public object CallStringFunction(string command, string paramStr)
{
    Type ScriptHelperType = this.GetType();
    MethodInfo theMethod = StockHelperType.GetMethod(command);
    object[] param = { paramStr };
    return (object)theMethod.Invoke(this, param);
}
public object CallStringFunction(string command, string paramStr)
{
    Type ScriptHelperType = this.GetType();
    MethodInfo theMethod = StockHelperType.GetMethod(command);
    object[] param = { paramStr };
    return (object)theMethod.Invoke(this, param);
}

How to call methods from string names

To make use of CallStringFunction(), let analyze the following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class ScriptHelper
{
 
public object CallStringFunction(string command, string paramStr)
{
    Type ScriptHelperType = this.GetType();
    MethodInfo theMethod = StockHelperType.GetMethod(command);
    object[] param = { paramStr };
    return (object)theMethod.Invoke(this, param);
}
//And a method that will be called.
public int GiveMeValue(int input)
{
    return 100;
}
 
//To call GiveMeValue() by its string name, consider the following lines
public void TestMe()
{
    //Since CallStringFunction() returns a object type result, you can cast it to any type of value. In this example, I cast it to int.
    //I successfully tried to cast it to different types, such as double[], int[], Tuple<double, double...>
    int myResult = (int)CallStringFunction("GiveMeValue","100");
}
 
}
public class ScriptHelper
{

public object CallStringFunction(string command, string paramStr)
{
    Type ScriptHelperType = this.GetType();
    MethodInfo theMethod = StockHelperType.GetMethod(command);
    object[] param = { paramStr };
    return (object)theMethod.Invoke(this, param);
}
//And a method that will be called.
public int GiveMeValue(int input)
{
    return 100;
}

//To call GiveMeValue() by its string name, consider the following lines
public void TestMe()
{
    //Since CallStringFunction() returns a object type result, you can cast it to any type of value. In this example, I cast it to int.
    //I successfully tried to cast it to different types, such as double[], int[], Tuple<double, double...>
    int myResult = (int)CallStringFunction("GiveMeValue","100");
}

}

Call a function from a class instance

The above code shows you how to call a method within a class, so you don’t need to create a class instance. This section will show how to call a method from its name using its class instance.

1
2
3
4
5
6
7
8
public object CallByName(string command, string paramStr)
{
    Type ScriptHelperType = this.GetType();
    MethodInfo theMethod = StockHelperType.GetMethod(command);
    object[] param = { paramStr };
    ScriptHelper classInstance = new ScriptHelper();
    return (object)theMethod.Invoke(classInstance, param);
}
public object CallByName(string command, string paramStr)
{
    Type ScriptHelperType = this.GetType();
    MethodInfo theMethod = StockHelperType.GetMethod(command);
    object[] param = { paramStr };
    ScriptHelper classInstance = new ScriptHelper();
    return (object)theMethod.Invoke(classInstance, param);
}

Did you notice the new line, ScriptHelper classInstance = new ScriptHelper();? This line is to create an instance of ScriptHelper class. The instance, classInstance, is used to invoke the real function.

A fully functional C# Script Parser
Note: If you want a working and neat C# script parser, you can try CS-Script by Oleg Shilo from this site. I didn’t try the library, but after reading its help file, I could see a lot uses of it.

 

About The Author

Hao Nguyen

Hao Nguyen

Hello! I'm Hao Nguyen and I'm currently working as a Network Engineer for a small firm in Houston. I enjoy writing technical documents and blog, such as PlanTutorials.com

Leave a Reply