在.net中使用javascript

以前我一直使用Jint来实现在.net中解析javascript,Jint非常好用,但不是很完善,时常遇到一些不支持的属性和bug,一旦踩雷后排查和规避起来往往比较麻烦。

今天发现了一个更完善的Javascript库——Javascript .NET。试用了一下,貌似比Jint更为完善一些,使用的方式和Jint差不多,以后就用它了。

 1 // Initialize a context
 2 JavascriptContext context = new JavascriptContext();
 3 
 4 // Setting external parameters for the context
 5 context.SetParameter("console"new SystemConsole());
 6 context.SetParameter("message""Hello World !");
 7 context.SetParameter("number"1);
 8 
 9 // Script
10 string script = @"
11     var i;
12     for (i = 0; i < 5; i++)
13         console.Print(message + ' (' + i + ')');
14     number += i;
15 ";
16 
17 // Running the script
18 context.Run(script);
19 
20 // Getting a parameter
21 Console.WriteLine("number: " + context.GetParameter("number"));

可以在这里下载编译好了的DLL文件。

原文地址:https://www.cnblogs.com/TianFang/p/2379246.html