SA Developer .NET

Execute a JavaScript function in a C# application.

rated by 0 users
This post has 2 Replies | 0 Followers

Top 10 Contributor
Male
Posts 540
Moderator
ProfK Posted: Thu, Nov 6 2008 13:16
Without using a WebBrowser control, how could I execute a JavaScript function, that is decoupled from any browser DOM dependencies, in a C# application? I am investigating implementing validation rules only once, for both server and client validation.  Are there any JS interpretor libraries I could use?
Top 50 Contributor
Male
Posts 52

Have a look at the Windows Script Control, it's a bit clunky and documentation is scarce but it's reasonably easy to figure out. The version I used didn’t support full JavaScript (at least not 1.2); only Jscript - but perhaps this has been improved in later versions.

Download Windows Script Control 

Simple tutorial


Top 75 Contributor
Posts 33

Try this:

 

    public class Evaluator
    {
        public static int EvalToInteger(string statement)
        {
            string s = EvalToString(statement);
            return int.Parse(s.ToString());
        }

        public static double EvalToDouble(string statement)
        {
            string s = EvalToString(statement);
            return double.Parse(s);
        }

        public static string EvalToString(string statement)
        {
            object o = EvalToObject(statement);
            return o.ToString();
        }
        public static bool EvalToBool(string statement)
        {
            object o = EvalToObject(statement);
            return bool.Parse(o.ToString());
        }
        public static object EvalToObject(string statement)
        {
            return _evaluatorType.InvokeMember(
                        "Eval",
                        BindingFlags.InvokeMethod,
                        null,
                        _evaluator,
                        new object[] { statement }
                     );
        }

        static Evaluator()
        {
            ICodeCompiler compiler;
            compiler = new JScriptCodeProvider().CreateCompiler();

            CompilerParameters parameters;
            parameters = new CompilerParameters();
            parameters.GenerateInMemory = true;

            CompilerResults results;
            results = compiler.CompileAssemblyFromSource(parameters, _jscriptSource);

            Assembly assembly = results.CompiledAssembly;
            _evaluatorType = assembly.GetType("Evaluator.Evaluator");

            _evaluator = Activator.CreateInstance(_evaluatorType);
        }

        private static object _evaluator = null;
        private static Type _evaluatorType = null;
        private static readonly string _jscriptSource =

            @"package Evaluator
            {
               class Evaluator
               {
                  public function Eval(expr : String) : String
                  {
                     return eval(expr);
                  }
               }
            }";
    }

Page 1 of 1 (3 items) | RSS