// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Reflection; using System.CodeDom.Compiler; using Microsoft.JScript; namespace E { public interface IJS { double Eval(string expr); } public class JS { private static IJS engine; static JS() { string src = "import System; import E; public class C implements IJS { public function Eval(expr : String) : Double { return eval(expr); } }"; CodeDomProvider comp = new JScriptCodeProvider(); CompilerParameters param = new CompilerParameters(); param.GenerateInMemory = true; param.ReferencedAssemblies.Add("System.dll"); param.ReferencedAssemblies.Add(System.Reflection.Assembly.GetExecutingAssembly().Location); CompilerResults res = comp.CompileAssemblyFromSource(param, src); Assembly asm = res.CompiledAssembly; engine = (IJS)asm.CreateInstance("C"); } public static double Eval(string expr) { return engine.Eval(expr); } } public class Program { public static void Main(string[] args) { Console.WriteLine(JS.Eval("23.4 * 3 - 3")); } } }