在.NET桌面和浏览器之间共享JavaScript代码

发布于 2024-07-08 17:07:46 字数 669 浏览 7 评论 0原文

我有一组核心、复杂的 JavaScript 数据结构/类,我希望能够在浏览器中将其用作 JavaScript,并通过 .NET 3.5 在桌面上运行。 是否可以将 Web 友好的 JavaScript 编译成我的 C# 代码可以访问的程序集?

  • 托管 JScript - 是否有一个可用的编译器可以生成可以在桌面 CLR 上运行的东西? 我只见过 Silverlight 运行时的已发布示例。 听起来它并没有在桌面上正式可用,但我愿意尝试一些不太幸运的事情,比如我可以使用 Silverlight 工具进行编译,然后在桌面 CLR 上运行吗?
  • JScript .NET - 听起来它有足够的自定义语言扩展,在浏览器中运行相同的代码确实很困难,但也许有办法......?

如果这些都不起作用,我想我的选择是:

  • 拥有一个 C# 版本和一个 JavaScript 版本,我必须保持同步(恶心)。
  • 编写一个在编译时运行的预处理器,将我的 JavaScript 转换为 JScript .NET 或我可以编译为 .NET 程序集的东西。
  • Script#:看起来这会将 C# 代码转换为 JavaScript 代码。 不太好,因为我更喜欢能够直接大量调整 JavaScript 代码,但它可以工作。

有人通过这些选项取得成功吗?

I have a set of core, complicated JavaScript data structures/classes that I'd like to be able to use both in the browser as JavaScript and run on the desktop with .NET 3.5. Is it possible to compile web-friendly JavaScript into assemblies that my C# code can access?

  • Managed JScript - Is there a compiler for this available that will produce something that can run on the desktop CLR? I've only seen released examples for the Silverlight runtime. It doesn't sound like it's officially available on the desktop but I'm willing to try something less blessed, like can I compile with the Silverlight tools and then run on the desktop CLR?
  • JScript .NET - Sounds like it's got enough custom language extensions where having the same code run in a browser would be really hard, but maybe there's a way...?

If neither of those work, I guess my options are to:

  • Have a C# version and a JavaScript version which I have to keep in sync (yuck).
  • Write a preprocessor of sorts that runs at compile time to turn my JavaScript into JScript .NET or something that I can compile into .NET assemblies.
  • Script#: Looks like this turns C# code into JavaScript code. Not awesome since I'd prefer to be able to heavily tune JavaScript code directly, but it could work.

Anyone have success with any of these options?

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

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

发布评论

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

评论(3

伪心 2024-07-15 17:07:46

JScript.Net 实际上主要兼容 EMCAScript3。 只要您将代码视为处理库(仅让它进行运算等 - 使用回调与程序交互),您应该没问题。
我正在做类似的事情,使用 javascript 作为我的核心解析库,然后在 python、dotnet 和 php 中使用它。

JScript.Net is actually mostly EMCAScript3 compliant. As long as you treat your code as a processing library (only have it do crunching, etc - use callbacks to interact with the program), you should be fine.
I'm doing something similar, using javascript as my core parsing library, and then using it within python, dotnet, and php.

百思不得你姐 2024-07-15 17:07:46

托管 JavaScript (IronJScript) 似乎已被终止。 请参阅此答案

Managed JavaScript (IronJScript) appears to have been killed. See this answer:

旧瑾黎汐 2024-07-15 17:07:46

另一种选择怎么样:

  • 通过 winforms 的 WebBrowser 控件在桌面应用程序中托管 javascript 应用程序?

这可能吗?

您还可以从 WebBrowser 控件访问 DOM,但并非完全支持您想要的所有内容。

How about another option:

  • Host the javascript application inside your desktop app via winforms' WebBrowser control?

Is this possible?

You can also access the DOM from WebBrowser control but not everything you want is fully supported.

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