将 ECMA Script 集成到 C# 程序中,最快的技术是什么?

发布于 11-30 09:14 字数 195 浏览 5 评论 0原文

我正在尝试在 C# 程序中执行 JavaScript,并且正在尝试确定:

为 JavaScript 创建状态机会更好,还是应该在执行之前将其编译为 MSIL?

我真的在寻找技术而不是解决方案。一些人尝试编译 JavaScript,将其视为一种编程语言,另一些人则使用状态机并将其视为一种脚本语言。

也欢迎任何有关执行方法的文档。

I'm trying to execute JavaScript in a C# program, and I'm trying to determine:

Would it be better to create a state machine for JavaScript, or should it be compiled into MSIL before executing it?

I'm really looking for techniques rather than solutions. Some attempt to compile the JavaScript, there by treating it as a programming language, others use a state machine and treat it as it was meant to be : a scripting language.

Any documentation on the methods used for executing is welcome also.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉生梦死2024-12-07 09:14:11

在 C# 程序中运行 JS 的最简单方法是使用现有的 .Net JS 运行时之一:IronJS、Jurassic 和 Jint。如果您有兴趣添加另一个基于 .Net 的解释器,我将从检查它们的代码开始;如果您要在程序中嵌入 JS,那么使用预先存在的 JS 可能是最佳选择。

The easiest way to run JS in a C# program is to use one of the existing .Net JS runtimes: IronJS, Jurassic, and Jint. If you're interested in adding another .Net-based interpreter to the mix, examining their code is where I'd start; If you're embedding JS in your program then using a preexisting one is probably the way to go.

潦草背影2024-12-07 09:14:11

您还没有解释您的场景 - 有多少 javascript、它执行的频率、它更改的频率、它需要与 C# 逻辑的集成程度如何,等等。

如果它很少改变,那么通过替代方案之一将其编译成 MSIL 可能是明智之举。
如果它“不断”变化,那么建立一个 JS 编译器并根据需要向其发送 JS 可能是有意义的。它类似于 Javascript 的 REPL,您可以通过编程方式使用它。 Node.js 提供了一个可以执行 REPL 操作的服务器;它可以监听一个端口,你向它发送 JS 进行评估,然后它返回结果。有免费的 w3sockets 组件 可以从 cscript.exe 中使用它来执行类似的操作:例如,您可以构建一个“主机”Javascript shell,它侦听套接字,执行它接收到的 js,然后发回结果。

如果它需要与 C# 代码紧密集成,那么您必须对此更加聪明。一种方法可能是将 JS 托管在 Windows 脚本组件 并让 C# 代码通过 COM 调用 JS 逻辑。您也可以执行相反的操作 - 让 JS 主机通过 COM 调用 C# 层来收集输入并交付结果。


编辑
更好的方法是使用 IActiveScript 东西 - 这是 Microsoft 使在应用程序中托管脚本成为可能的官方方法。有一个发布的 winforms 示例 在这里

或者更好,请参阅 通过 C# 解析和执行 JS

使用我放在一起的代码演示如何使用 IActiveScript 从 .NET 应用程序解析和调用 Javascript。

在此处输入图像描述

您可以为此应用下载 VS2010 项目 此处

You haven't explained your scenario - how much javascript, how often does it get executed, how often does it change, how closely integrated it needs to be with the C# logic, and so on.

If it rarely changes, then it may be smart to compile it into MSIL via one of the alternatives.
If it changes "constantly" then it might make sense to stand up a JS compiler and send it JS as necessary. It's something like a REPL for Javascript, that you'd use programmatically. Node.js offers a server that can do REPL operations; it could listen on a port, you send it JS to evaluate, and it sends back the result. There's a free w3sockets component that could be used from cscript.exe to do something similar: for example you could build a "host" Javascript shell that listens on a socket, executes the js it receives, then sends back the result.

If it needs to be closely integrated with the C# code, then you'll have to be smarter about this. One way to do it might be to host the JS in a Windows Script Component and have the C# code invoke the JS logic via COM. You can also do the converse - have the JS host invoke the C# layer via COM to gather input and deliver results.


EDIT
A better way is to use the IActiveScript stuff - this is the official way Microsoft makes it possible to host scripting within an application. There's a winforms example published here.

OR better, see parse and execute JS by C#

Using that code I put together a demonstration of how to parse and invoke Javascript from a .NET app, using IActiveScript.

enter image description here

You can download a VS2010 project for this app here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文