从 C# 程序集中执行 JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/67734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:04:07  来源:igfitidea点击:

Execute JavaScript from within a C# assembly

提问by ScottKoon

I'd like to execute JavaScript code from within a C# assembly and have the results of the JavaScript code returned to the calling C# code.

我想从 C# 程序集中执行 JavaScript 代码,并将 JavaScript 代码的结果返回给调用 C# 代码。

It's easier to define things that I'm not trying to do:

定义我不想做的事情更容易:

  • I'm not trying to call a JavaScript function on a web page from my code behind.

  • I'm not trying to load a WebBrowser control.

  • I don't want to have the JavaScript perform an AJAX call to a server.

  • 我不想从我的代码后面调用网页上的 JavaScript 函数。

  • 我不是要加载 WebBrowser 控件。

  • 我不想让 JavaScript 对服务器执行 AJAX 调用。

What I want to do is write unit tests in JavaScript and have then unit tests output JSON, even plain text would be fine. Then I want to have a generic C# class/executible that can load the file containing the JS, run the JS unit tests, scrap/load the results, and return a pass/fail with details during a post-build task.

我想做的是用 JavaScript 编写单元测试,然后单元测试输出 JSON,即使是纯文本也可以。然后我想要一个通用的 C# 类/可执行文件,它可以加载包含 JS 的文件,运行 JS 单元测试,废弃/加载结果,并在构建后任务期间返回包含详细信息的通过/失败。

I think it's possible using the old ActiveX ScriptControl, but it seems like there ought to be a .NET way to do this without using SilverLight, the DLR, or anything else that hasn't shipped yet. Anyone have any ideas?

我认为使用旧的 ActiveX ScriptControl 是可能的,但似乎应该有一种 .NET 方式来做到这一点,而无需使用 SilverLight、DLR 或任何其他尚未发布的东西。谁有想法?

update: From Brad Abrams blog

更新:来自 Brad Abrams 博客

namespace Microsoft.JScript.Vsa
{
    [Obsolete("There is no replacement for this feature. " +
              "Please see the ICodeCompiler documentation for additional help. " +
              "http://go.microsoft.com/fwlink/?linkid=14202")]

Clarification: We have unit tests for our JavaScript functions that are written in JavaScript using the JSUnit framework. Right now during our build process, we have to manually load a web page and click a button to ensure that all of the JavaScript unit tests pass. I'd like to be able to execute the tests during the post-build process when our automated C# unit tests are run and report the success/failure alongside of out C# unit tests and use them as an indicator as to whether or not the build is broken.

说明:我们对使用 JSUnit 框架用 JavaScript 编写的 JavaScript 函数进行了单元测试。现在在我们的构建过程中,我们必须手动加载一个网页并单击一个按钮以确保所有 JavaScript 单元测试都通过。当我们的自动化 C# 单元测试运行时,我希望能够在构建后过程中执行测试,并在 C# 单元测试的同时报告成功/失败,并将它们用作指示是否构建被打破。

采纳答案by user10917

You can run your JSUnit from inside Nant using the JSUnit server, it's written in java and there is not a Nant task but you can run it from the command prompt, the results are logged as XML and you can them integrate them with your build report process. This won't be part of your Nunit result but an extra report. We fail the build if any of those test fails. We are doing exactly that using CC.Net.

您可以使用 JSUnit 服务器从 Nant 内部运行您的 JSUnit,它是用 Java 编写的,并且没有 Nant 任务,但您可以从命令提示符运行它,结果记录为 XML,您可以将它们与您的构建报告集成过程。这不会成为您的 Nunit 结果的一部分,而是一份额外的报告。如果这些测试中的任何一个失败,我们就会使构建失败。我们正在使用 CC.Net 做到这一点。

回答by Gulzar Nazim

You can use the Microsoft Javascript engine for evaluating JavaScript code from C#

您可以使用 Microsoft Javascript 引擎从 C# 评估 JavaScript 代码

Update: This is obsolete as of VS 2008

更新:自 VS 2008 起这已过时

回答by Sam Wessel

Could it be simpler to use JSUnitto write your tests, and then use a WatiNtest wrapper to run them through C#, passing or failing based on the JSUnit results?

使用JSUnit编写测试,然后使用WatiN测试包装器通过 C# 运行它们,根据 JSUnit 结果通过或失败是否更简单?

It is indeed an extra step though.

不过,这确实是一个额外的步骤。

I believe I read somewhere that an upcoming version of either MBUnit or WatiN will have the functionality built in to process JSUnit test fixtures. If only I could remember where I read that...

我相信我在某处读到 MBUnit 或 WatiN 的即将推出的版本将具有内置的功能来处理 JSUnit 测试装置。如果我能记得我在哪里读到的那...

回答by tomasr

I don't know of any .NET specific way of doing this right now... Well, there's still JScript.NET, but that probably won't be compatible with whatever JS you need to execute :)

我现在不知道任何 .NET 特定的方法来做这件事......好吧,仍然有 JScript.NET,但它可能与你需要执行的任何 JS 不兼容 :)

Obviously the future would be the .NET JScript implementation for the DLR which is coming... someday (hopefully).

显然,未来将是即将到来的 DLR 的 .NET JScript 实现......总有一天(希望如此)。

So that probably leaves running the old ActiveX JScript engine, which is certainly possible to do so from .NET (I've done it in the past, though it's a bit on the ugly side!).

所以这可能会导致运行旧的 ActiveX JScript 引擎,这当然可以从 .NET 执行(我过去已经这样做了,虽然它有点丑陋!)。

回答by scubabbl

The code should be pretty self explanitory, so I'll just post that.

代码应该是很自我解释的,所以我会发布它。

<add assembly="Microsoft.Vsa, Version=8.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies>


using Microsoft.JScript;

public class MyClass {

    public static Microsoft.JScript.Vsa.VsaEngine Engine = Microsoft.JScript.Vsa.VsaEngine.CreateEngine();

    public static object EvaluateScript(string script)
    {
        object Result = null;
        try
        {
            Result = Microsoft.JScript.Eval.JScriptEvaluate(JScript, Engine);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }

        return Result;
    }

    public void MyMethod() {
        string myscript = ...;
        object myresult = EvaluateScript(myscript);
    }
}

回答by Jon Galloway

If you're not executing the code in the context of a browser, why do the tests need to be written in Javascript? It's hard to understand the bigger picture of what you're trying to accomplish here.

如果您不是在浏览器的上下文中执行代码,为什么需要用 Javascript 编写测试?很难理解你在这里试图完成的更大的图景。