JScript Eval Method In C#

JavaScript and JScript languages provide a convenient Eval method that allows for dynamic evaluation of expressions. This functionality can be duplicated in C# using Microsoft’s JScript CodeDomProvider and Reflection.
Following class automatically compiles an in-memory assembly using JScript CodeDomProvider. The Eval method uses this assembly to dynamically evaluate specified expressions:
using System;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.JScript;
///
/// Implements JScript/JavaScript Eval like
/// functionality in C# to allow dynamic evaluation
/// of expressions.
///
public static class ExpressionEvaluator
{
#region Private Members
private static Type _evaluatorType;
private static object _evaluatorInstance;
private static readonly string _jscriptEvalClass =
@"import System;
class JScriptEvaluator
{
public static function Eval(expression : String) : String
{
return eval(expression);
}
}";
#endregion
#region Private Methods
///
/// Creates a dynamic in-memory assembly using JScript.NET
/// to evaluate expressions.
///
private static void Initialize()
{
CodeDomProvider compiler = new JScriptCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
parameters.ReferencedAssemblies.Add("system.dll");
CompilerResults results = compiler.CompileAssemblyFromSource(parameters, _jscriptEvalClass);
Assembly assembly = results.CompiledAssembly;
_evaluatorType = assembly.GetType("JScriptEvaluator");
_evaluatorInstance = Activator.CreateInstance(_evaluatorType);
}
#endregion
#region Public Methods
///
/// Evaluates specified expression using reflection
/// on the assembly generated in Initialize() method.
///
/// The expression.
/// A string representing evaluated expression.
public static string Eval(string expression)
{
if (_evaluatorInstance == null)
Initialize();
object result = _evaluatorType.InvokeMember(
"Eval",
BindingFlags.InvokeMethod,
null,
_evaluatorInstance,
new object[] { expression }
);
return (result == null) ? null : result.ToString();
}
#endregion
}
Using this class, we can execute statements such as:
Console.WriteLine(ExpressionEvaluator.Eval("2 + 9"));
Console.WriteLine(ExpressionEvaluator.Eval("System.DateTime"));
Console.WriteLine(ExpressionEvaluator.Eval("Environment.OSVersion.VersionString"));
