C# 解释器(无需编译)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1660452/
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-06 19:46:44  来源:igfitidea点击:

C# Interpreter (without compilation)

c#scripting

提问by Manitra Andriamitondra

Is there a ready-to-use C# interpreter out there, that is does not rely on runtime compilation?

是否有现成的 C# 解释器,它不依赖于运行时编译?

My requirements are :

我的要求是:

  • A scripting engine
  • Must Handle C# syntax
  • Must work on medium-trust environments
  • Must not use runtime compilation (CodeDomProvider ...)
  • Open source (or at least free of charge both for personal and professional use)
  • 一个脚本引擎
  • 必须处理 C# 语法
  • 必须在中等信任的环境中工作
  • 不得使用运行时编译(CodeDomProvider ...)
  • 开源(或至少免费供个人和专业使用)

If this is not clear, I need something like Jint (http://jint.codeplex.com/), but which allows me to write C# scripts instead of JavaScript ones.

如果这不清楚,我需要类似 Jint ( http://jint.codeplex.com/) 的东西,但它允许我编写 C# 脚本而不是 JavaScript 脚本。

Thanks for your help.

谢谢你的帮助。

采纳答案by Mitch Wheat

Have you looked at paxScript.NET?

你看过paxScript.NET吗?

回答by Marc Wittke

or http://www.csscript.net/Oleg was writing a good intro at code project

http://www.csscript.net/Oleg 在代码项目中写了一个很好的介绍

回答by Luke Quinane

Check out the Monoproject. They recently demoed CsharpReplwhich sounds like what you're after. The PDC 2008 video here.

查看Mono项目。他们最近演示了CsharpRepl,这听起来像您所追求的。PDC 2008 视频在这里



Update:更新:


仔细看似乎使用Mono.CSharpMono.CSharp服务来评估脚本将是不可能的。目前它链接到 Mono 运行时,他们不希望它在中等信任环境中运行。看this discussion此讨论以获取更多信息。

On alternative possibility is to include the Mono C# compiler(sources here) in your project and use it to generate assemblies that you load from the file system. It you are worried about the resources required to load all those assemblies you might have to load them in a separate AppDomain.

另一种可能性是在您的项目中包含Mono C# 编译器此处的源代码)并使用它来生成您从文件系统加载的程序集。如果您担心加载所有这些程序集所需的资源,您可能需要将它们加载到单独的 AppDomain 中。

回答by Lee

It doesn't handle exact C# syntax, but PowerShell is so well enmeshed with the .NET framework and is such a mature product, I think you would be unwise to ignore it as at least a possible solution. Most server products being put out by Microsoft are now supporting PowerShell for their scripting interface including Microsoft Exchange and Microsoft SQL Server.

它不处理精确的 C# 语法,但 PowerShell 与 .NET 框架如此紧密地结合在一起并且是一个如此成熟的产品,我认为您至少将它作为一个可能的解决方案而忽略它是不明智的。Microsoft 推出的大多数服务器产品现在都支持 PowerShell 作为其脚本接口,包括 Microsoft Exchange 和 Microsoft SQL Server。

回答by dodecaphonic

I believe Mono has mint, an interpreter they use before implementing the JIT for a given platform. While the docs in the official site (e.g. Runtime) say it's just an intermediate state before consolidating the jitting VM, I'm pretty sure it was there the last time I compiled it on Linux. I can't quite check it right now, unfortunately, but maybe it's in the direction you want.

我相信 Mono 有mint,这是他们在为给定平台实现 JIT 之前使用的解释器。虽然官方站点中的文档(例如Runtime)说这只是整合 jitting VM 之前的一个中间状态,但我很确定它是我上次在 L​​inux 上编译它时存在的。不幸的是,我现在无法完全检查它,但也许它在您想要的方向上。

回答by Trav L

Is Snippet Compilersomething you looking for?

摘录编译你找什么东西?

回答by Jason Marcell

LINQPadcan work as a code snippet IDE. The application is very small and lightweight. It is free (as in beer) but not open-source. Autocompletion costs extra but not much ($19).

LINQPad可以作为代码片段 IDE。该应用程序非常小巧轻便。它是免费的(就像啤酒一样)但不是开源的。自动完成费用额外但不多(19 美元)。

Edit: after reading over the comments in this post a little more carefully, I don't think LINQPad is what you want. You need something that can programmatically evaluate thousands of little scripts dynamically, right? I did this at work using Iron Ruby very easily. If you're willing to use a DLR language, this would probably be more feasible. I also did some similar work with some code that could evaluate a C# lambda expression passed in as a string but that was extremely limited.

编辑:仔细阅读这篇文章中的评论后,我认为 LINQPad 不是你想要的。您需要能够以编程方式动态评估数千个小脚本的东西,对吗?我在工作中很容易地使用 Iron Ruby 做到了这一点。如果您愿意使用 DLR 语言,这可能更可行。我还使用一些可以评估作为字符串传入的 C# lambda 表达式的代码做了一些类似的工作,但这非常有限。

回答by mfeingold

I faced the same problem. In one project I was looking to provide a generic way to specify conditions controlling when a certain letter has to be generated. In another project the conditions were controlling how cases were assigned to queues. In both of them The following solution worked perfectly:

我遇到了同样的问题。在一个项目中,我希望提供一种通用方法来指定控制何时必须生成某个字母的条件。在另一个项目中,条件控制着如何将案例分配到队列中。在他们两个以下解决方案完美地工作:

  1. The Language for the snippets - I chose JScript so that I do not have to worry about variable types.
  2. The Compilation - yes it requires full trust, but you can place your code in a separate assembly and give it full trust. Do not forget to mark it with AllowPartiallyTrustedCaller attribute.
  3. Number of code snippets - I treated every snippet as a method, not a class. This way multiple methods can be combined into a single assembly
  4. Disk usage - I did all compilation in memory without saving the assembly to disk. It also helps if you need to reload it.
  1. 片段的语言 - 我选择了 JScript,这样我就不必担心变量类型。
  2. 编译 - 是的,它需要完全信任,但您可以将代码放在单独的程序集中并给予完全信任。不要忘记用 AllowPartiallyTrustedCaller 属性标记它。
  3. 代码片段的数量 - 我将每个片段视为一个方法,而不是一个类。这样可以将多种方法组合成一个程序集
  4. 磁盘使用 - 我在内存中进行了所有编译,而没有将程序集保存到磁盘。如果您需要重新加载它,它也有帮助。

All of this works in production without any problems

所有这些都可以在生产中正常工作,没有任何问题

Edit

编辑

Just to clarify 'snippet' - The conditions I am talking about are just boolean expressions. I programatically add additional text to turn it to methods and methods to compilable classes.

只是为了澄清“片段” - 我正在谈论的条件只是布尔表达式。我以编程方式添加附加文本以将其转换为可编译类的方法和方法。

Also I can do the same with C# although I still think JScript is better for code snippets

我也可以用 C# 做同样的事情,虽然我仍然认为 JScript 更适合代码片段

And BTW my code is open source feel free to browse. Just keep in mind there is a lot of code there unrelated to this discussion. Let me know if you need help to locate the pieces concerning the topic

顺便说一句,我的代码是开源的,可以随意浏览。请记住,有很多与此讨论无关的代码。如果您需要帮助来查找与该主题相关的部分,请告诉我

回答by Jason

I need to evaluate 10000+ small scripts that are all differents, compiling all of them would be just dramatically slow

我需要评估 10000 多个不同的小脚本,编译所有这些脚本会非常慢

Interpretting these would be even more painfully slow. We have a similar issue that we address as follows:

解释这些会更加缓慢。我们有一个类似的问题,我们解决如下:

We use the Gold Parser project to parse source code and convert it to an XML based 'generic language'. We run this through a transform that generates VB.Net source code (simply because it's case insensitive). We then compile these using the .Net runtime into a standalone DLL, and call this using heavily restricted access.

我们使用 Gold Parser 项目来解析源代码并将其转换为基于 XML 的“通用语言”。我们通过一个生成 VB.Net 源代码的转换来运行它(仅仅是因为它不区分大小写)。然后我们使用 .Net 运行时将它们编译成一个独立的 DLL,并使用严格限制的访问来调用它。

It sounds as though you are creating something like a dynamic website where people can create custom modules or snippets of functionality, but using C# to do this introduces a couple of main problems; C# has to be compiled, and the only way around this is to interpet it at runtime, and this is unfeasible, and even if you do compile each snippet then you end up with 10,000 DLLs, which is impractical and unusable.

听起来好像您正在创建类似动态网站的东西,人们可以在其中创建自定义模块或功能片段,但是使用 C# 来执行此操作会引入几个主要问题;C# 必须被编译,解决这个问题的唯一方法是在运行时插入它,这是不可行的,即使你编译了每个片段,你最终也会得到 10,000 个 DLL,这是不切实际和无法使用的。

If your snippets are rarely changing, then I would consider programatically wrapping them into a single set of source, with each having a unique name, then compile them in a single shot (or as a timed process every 10mins?). This is what we do, as it also allows 'versioning' of peoples sessions so they continue using the version of DLL they had at the start of their session, but when every session stops using an old version then it's removed.

如果您的片段很少更改,那么我会考虑以编程方式将它们包装到一组源中,每个源都有一个唯一的名称,然后一次性编译它们(或作为每 10 分钟的定时过程?)。这就是我们所做的,因为它还允许对人们的会话进行“版本控制”,以便他们继续使用他们在会话开始时拥有的 DLL 版本,但是当每个会话停止使用旧版本时,它就会被删除。

If your snippets change regularly throughout the day then I would suggest you look at an interpretted scripting language instead, even PHP, and mix your languages depending on the functionality you require. Products such as CScript and LinqPad all use the CodeDomProvider, because you have to have IMSL somewhere if you want to program compiled logic.

如果您的代码段全天定期更改,那么我建议您改用解释性脚本语言,甚至是 PHP,并根据您需要的功能混合使用您的语言。CScript 和 LinqPad 等产品都使用 CodeDomProvider,因为如果要对编译逻辑进行编程,就必须在某处安装 IMSL。

The only other option is to write your own interpretter and use reflection to access all the other libraries you need to access, but this is extremely complex and horrible.

唯一的另一种选择是编写自己的解释器并使用反射来访问您需要访问的所有其他库,但这非常复杂和可怕。

As your requirements are effectively unachievable, I would suggest you take a step back and figure out a way of removing one or more restrictions. Whether you find a FullTrust environment to compile your snippets in, remove the need for full code support (i.e. move to interpretted code snippet support), or even change the whole framework to something non .Net.

由于您的要求实际上无法实现,我建议您退后一步,找出一种消除一个或多个限制的方法。无论您是找到一个 FullTrust 环境来编译您的代码片段,还是不再需要完整的代码支持(即转向解释代码片段支持),或者甚至将整个框架更改为非 .Net 的东西。

回答by Burcu Co

bungee# is the thing that you want, in a short time, bungee sharp will be an open source project in

bungee# 就是你想要的东西,在短时间内,bungeeSharp 将成为一个开源项目

http://www.crssoft.com/Services/Bungee

http://www.crssoft.com/Services/Bungee

. you can create scripts with the same c# syntaxt. there is no assembly creation when you run the script, interpretation is done on the fly, so the performance is high. all the keywords are available like c#. I hope u will like it very much..

. 您可以使用相同的 c# 语法创建脚本。运行脚本时没有程序集创建,解释是即时完成的,因此性能很高。所有关键字都可以像c#一样使用。我希望你会非常喜欢它..