如何在C#中使用Jint加载JavaScript文件? [英] How do I load a JavaScript file with Jint in C#?

查看:1382
本文介绍了如何在C#中使用Jint加载JavaScript文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Jint加载JavaScript文件,但我似乎无法弄明白。文档说我可以做一些像 engine.run(file1),但它似乎没有加载任何文件。我是否需要对文件名做些特别的事情?

I want to load a JavaScript file using Jint, but I can't seem to figure it out. The documentation said I could do something like engine.run(file1), but it doesn't appear to be loading any files. Do I need to do something special with the file name?

这是我的JavaScript文件:

Here's my JavaScript file:

// test.js
status = "test";

这是我的C#

JintEngine js = new JintEngine();
js.Run("test.js");
object result = js.Run("return status;");
Console.WriteLine(result);
Console.ReadKey();

如果我在手动输入代码>运行它有效。

object result = js.Run("return 2 * 21;"); // prints 42


推荐答案

我通过手动加载找到了解决方法我自己将文件转换为文本,而不是按照此处提到的名称加载: http://jint.codeplex.com / discussion / 265939

I found a workaround by manually loading the file into text myself, instead of loading it by name as mentioned here: http://jint.codeplex.com/discussions/265939

        StreamReader streamReader = new StreamReader("test.js");
        string script = streamReader.ReadToEnd();
        streamReader.Close();

        JintEngine js = new JintEngine();

        js.Run(script);
        object result = js.Run("return status;");
        Console.WriteLine(result);
        Console.ReadKey();

这篇关于如何在C#中使用Jint加载JavaScript文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆