原理﹕
使用js.net(由於裏面有eval方法)編寫一個類﹐類別中新增一個方法來執行動態js代碼。
而後使用jsc.exe把它編譯成一個dll
在c#項目中把它加入﹐而後傳入動態代碼﹐呼叫這個類別的這個方法﹐獲得結果。
1.第一步﹐新增一個js文件
MyEval.jsc#
![](http://static.javashuo.com/static/loading.gif)
class MyEval{
![](http://static.javashuo.com/static/loading.gif)
function execute(code:String) :String { // Method.
![](http://static.javashuo.com/static/loading.gif)
return eval(code);
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
}
2.編譯成dll測試
![](http://static.javashuo.com/static/loading.gif)
jsc /target:library /out:MyEval.dll MyEval.js
3.編寫測試文件
TestEval.csspa
![](http://static.javashuo.com/static/loading.gif)
using System;
class test
{
public static void Main(){
string code = "var result:int =0;result==1?\"成功\":\"失敗\"";
MyEval eval = new MyEval();
string result = eval.execute(code);
Console.WriteLine("The Result is:" + result);
}
}