FANDOM


The simplest way to do expression evaluation from .NET program stills seems to be to use the JScript Eval function.

// Based on code from: http://odetocode.com/code/80.aspx
//
// Examples:
//      Console.WriteLine("{0}", Evaluator.TryEval("(9/6) + 3"));
//      Console.WriteLine("{0}", Evaluator.TryEval("7<8"));
//      Console.WriteLine("{0}", Evaluator.TryEval("((1==1 || 3>2) && (2!=1))"));
//
//      Console.WriteLine("{0}", Evaluator.TryEval("Math.round(9.99999) == 10"));
//
//      Console.WriteLine("{0}", Evaluator.TryEval("new Date(\"1/12/1966\").getFullYear() == 1966"));
//
//      Console.WriteLine("{0}", Evaluator.TryEval("(\"beth\" > \"abe\") || (\"tucan\" == \"sam\")"));
//      Console.WriteLine("{0}", Evaluator.TryEval("\"Lifeguard\".Contains(\"feguar\")"));
//      Console.WriteLine("{0}", Evaluator.TryEval("\"Lifeguard\".StartsWith(\"Life\")"));
//      Console.WriteLine("{0}", Evaluator.TryEval("\"Lifeguard\".EndsWith(\"guard\")"));
//      Console.WriteLine("{0}", Evaluator.TryEval("\"Lifeguard\".Substring(2,6) == \"feguar\""));
//      Console.WriteLine("{0}", Evaluator.TryEval("\"Lifeguard\".ToLower() == \"lifeguard\""));
//      Console.WriteLine("{0}", Evaluator.TryEval("\"Lifeguard\".IndexOf(\"feguar\") == 2"));
//      Console.WriteLine("{0}", Evaluator.TryEval("\"Lifeguard Lifeguard\".LastIndexOf(\"guard\") == 14"));
//      Console.WriteLine("{0}", Evaluator.TryEval("\"Lifeguard\".Remove(2,4) == \"Liard\""));
//      Console.WriteLine("{0}", Evaluator.TryEval("\"Lifeguard\".Replace(\"gua\",\"AAAA\") == \"LifeAAAArd\""));
//      Console.WriteLine("{0}", Evaluator.TryEval("\"Lifeguard\".Length == 9"));
//
//      Console.WriteLine("{0}", Evaluator.TryEval("\"Lifeguard\".match(/gua./) == \"guar\""));
//
// Add Reference Microsoft.Jscript.dll
//
// using System;
public class Evaluator
{
    public static object TryEval(string statement)
    {
        try
        {
            return Eval(statement);
        }
        catch (Microsoft.JScript.JScriptException e)
        {
            return string.Format("{0} at column {1} in \"{2}\"", e.Description, e.Column, e.LineText);
        }
    }

    public static object Eval(string statement)
    {
        try
        {
            return EvaluatorType.InvokeMember("Eval", System.Reflection.BindingFlags.InvokeMethod, null, EvaluatorInstance, new object[] { statement });
        }
        catch (System.Reflection.TargetInvocationException e)
        {
            var jscriptException = e.InnerException as Microsoft.JScript.JScriptException;
            if (jscriptException != null)
                throw jscriptException;

            throw;
        }
    }

    static Evaluator()
    {
        var parameters = new System.CodeDom.Compiler.CompilerParameters();
        parameters.GenerateInMemory = true;

        EvaluatorType = new Microsoft.JScript.JScriptCodeProvider().CreateCompiler().CompileAssemblyFromSource(parameters, JscriptSource).CompiledAssembly.GetType("Evaluator.Evaluator");
        EvaluatorInstance = Activator.CreateInstance(EvaluatorType);
    }

    private static readonly object EvaluatorInstance = null;
    private static readonly Type EvaluatorType = null;
    private const string JscriptSource =
        @"package Evaluator {
            class Evaluator {
                public function Eval(expr : String) : String { 
                    return eval(expr); 
                }
            }
        }";
}

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.