An Eval Function for C# using JScript.NET (JavaScript)

Once I was faced with a scenario where we needed client specific calculations applied to the results of a SQL query. The situation cried out for a “eval” type method where we could just plug values into a custom equation and retrieve a result. VBScript, JScript.NET, and JavaScript all contain an eval method, where you can pass a string containing a valid expression for the language and retrieve a result – dynamic execution.

Unfortunately, C# does not provide an eval method. One solution is to add a JScript.NET project to your solution. It feels a bit messy to suddenly have an extra assembly to build and deploy just for 10 lines of JScript code. An alternate solution is to use the System.CodeDom namespace to compile a JScript class in memory to use.

The C# class below contains all static methods. Using the evaluator is as simple as:

Evaluator.EvalToInteger(“(9/6) + 3”);

Source code follows. To compile, make sure you add a reference to the Microsoft.JScript assembly. Most of the work in this class happens during the static constructor when we compile a small piece of JScript.NET code. Once we have the code compiled we can reach inside the assembly and create an instance of the evaluation class. Since a static constructor will execute only once – your application will only perform this work the first time execution references a static member of the class.

With an object instance in hand, we use InvokeMember to dynamically invoke the method to perform evaluation. Links in the resources section will provide more information.

Update: Be careful using dynamic execution. There is always the risk of a code injection attack, particularly if the input comes from an untrusted source.