看到一个比较小巧的jint库。dll才244Kb的样子。在c#里面执行js脚本还蛮方便的。
1。执行js里的函数。
using Jint;
using System;
namespace RunJs
{
class Program
{
static void Main(string[] args)
{
Engine e = new Engine();
e.Execute("function add(a, b) { return a + b; }");
var s = e.Invoke("add",1,2).ToObject();
Console.WriteLine("{0}",s);
Console.ReadKey();
}
}
}
2.取得js里的对象值。
using Jint;
using System;
namespace RunJs
{
class Program
{
static void Main(string[] args)
{
Engine e = new Engine();
e.Execute("function add(a, b) { return a + b};var j=add(3,6);");
object s = e.GetValue("j").ToObject();