Summary of several ways in which C executes Javascript code

  • 2020-06-07 05:13:42
  • OfStack

1. Open source project Javascript. NET

Address: http: / / javascriptdotnet. codeplex. com/it is Google Chrome V8 engine in. NET encapsulation, functional, performance is relatively high, it is recommended to use! (Disadvantage is asp.net web application, it seems to report errors)

Sample code:


using Noesis.Javascript; 
using System; 
using System.Collections.Generic; 

namespace JsCSharp 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            using (JavascriptContext ctx = new JavascriptContext()) 
            { 
                var i = ctx.Run("1+2"); 
                Console.WriteLine(i);//3 
                ctx.Run("var d = new Date(2013,9-1,2,20,30,15)"); 
                var d = ctx.GetParameter("d"); 
                Console.WriteLine(d);//2013/9/2 20:30:15 
                var j = ctx.Run("function add(x,y){return x+y;};add(5,10);"); 
                Console.WriteLine(j);//15 
                ctx.Run("var obj = {};obj.name='jimmy';obj.sex='Male';obj.name=' Chun-ming Yang '"); 
                var obj = ctx.GetParameter("obj") as Dictionary<string, object>; 
                foreach (var key in obj.Keys) 
                { 
                    Console.WriteLine(string.Format("{0}:{1}", key, obj[key])); 
                    //name: Chun-ming Yang  
                    //sex:Male 
                } 
                var jsonArr = ctx.Run("[{Airport:'PEK',Name:' Beijing Capital Airport '},{Airport:'XIY',Name:' Xi 'an Xianyang Airport '}]") as Array; 
                foreach (var item in jsonArr) 
                { 
                    var json = item as Dictionary<string, object>; 
                    foreach (var key in json.Keys) 
                    { 
                        Console.WriteLine(string.Format("{0}:{1}", key, json[key])); 
                        //Airport:PEK 
                        //Name: Beijing Capital Airport  
                        //Airport:XIY 
                        //Name: Xi 'an Xianyang Airport  
                    } 
                } 
                ctx.SetParameter("x", 7); 
                ctx.Run("function add(x,y){return x+y;}; function sub(x,y){return x-y;};k = add(2,x);var t = sub(9,1);"); 
                var k = ctx.GetParameter("k"); 
                Console.WriteLine(k);//9 
                var t = ctx.GetParameter("t"); 
                Console.WriteLine(t);//8 
                Console.Read(); 
            } 

        } 

    } 
}

When deploying, note that es21EN100. dll and ES23en100. dll are copied to the bin directory

2. Use Microsoft. JScript Vsa

Add references to the Microsoft. JScript and Microsoft. Vsa2 assemblies, and you'll be ready, with the following example code:


using Microsoft.JScript; 
using Microsoft.JScript.Vsa; 
using System; 

namespace JsCSharp 
{ 
    class Program 
    { 
        static VsaEngine Engine = VsaEngine.CreateEngine(); 

        static void Main(string[] args) 
        { 
            var i = (int)EvalJScript("1+2;"); 
            Console.WriteLine(i);//3 
            var d = (DateObject)EvalJScript("var d = new Date(2013,9-1,2,20,30,15)"); 
            Console.WriteLine(d);//Mon Sep 2 20:30:15 UTC+8 2013 
            var j = EvalJScript("function add(x,y){return x+y;};add(5,10);"); 
            Console.WriteLine(j);//15 
            var obj = EvalJScript("var obj = {};obj.name='jimmy';obj.sex='Male';"); 
            Console.WriteLine(obj);//Male  Note: return last only 1 The result of a statement executed by  
            Console.Read(); 
        } 

  
        static object EvalJScript(string JScript) 
        { 
            object Result = null; 
            try
            { 
                Result = Microsoft.JScript.Eval.JScriptEvaluate(JScript, Engine); 
            } 
            catch (Exception ex) 
            { 
                return ex.Message; 
            } 
            return Result; 

        } 
    } 
}

Vsa has limited functionality, and when multiple statements are executed, only the result of the last one is returned.


Related articles: