如何在C#中使用Jint查找我的javascript函数?

时间:2017-08-14 11:30:10

标签: javascript c# jint

我在下面写了代码

static void Main(string[] args)
    {
        var engine = new Engine();
        var file = File.ReadAllText(@"...\Desktop\beautify.js");
        var final = file + Environment.NewLine + "(function () {return js_beautify('var a = 2;');})();";
        var exec = engine.Execute(final);
        var r = exec.GetCompletionValue().ToString();
        Console.WriteLine(r);
        Console.Read();

    }

但是Jint显示错误

Jint.Runtime.JavaScriptException: 'js_beautify is not defined'

任何人都可以帮我如何注册外部js库,如beautify.js,然后在Jint中调用它们的函数?

1 个答案:

答案 0 :(得分:1)

应该是

 var engine = new Engine().Execute("var global = {};").Execute(content);
 var js_beautify = engine.Execute("global.js_beautify").GetCompletionValue();
 var result = engine.Invoke(js_beautify, "var a = 2;").AsString();