如何使用C#创建自己的脚本引擎 [英] How create a my own script engine using c#

查看:673
本文介绍了如何使用C#创建自己的脚本引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


如何使用c#创建我自己的脚本引擎,
我有用C#编写的语法突出显示文本框,因此我需要脚本执行
请帮助我

Hi,
How to create a my own script engine using c#,
i have Syntax highlighting textbox written in C# for that i need script exective
please help me

推荐答案

这很有可能.

您可以使用CodeDOM来临时编译C#代码,方法是始终与可重新分发的.NET Framework的所有版本捆绑在一起的编译器.

请查看我过去的解决方案中的一些主要思想.您可以找到所有关键技术的骨架描述:
创建使用可重载插件的WPF应用程序... [ ^ ],
动态加载用户控件 [使用CodeDom生成代码 [ AppDomain拒绝加载程序集 [创建使用可重载插件的WPF应用程序... [ ^ ].

我在以上文章中考虑的某些方面可能出于您的目的而比较复杂.请不要害怕;您的情况可能更简单.请阅读并根据您的用例分析技术.

随意问问您的后续问题(请至少在看完这些帖子之后)—这个问题不太容易理解,但在设计和实现上也不是最困难的.

—SA
This is quite possible.

You can use CodeDOM to compile C# code on temporary basis using the compiler always bundled with all versions of re-distributable .NET framework.

Please see some main ideas in my past solutions. You can find skeleton descriptions of all key techniques:
Create WPF Application that uses Reloadable Plugins...[^],
Dynamically Load User Controls[^],
code generating using CodeDom[^],
AppDomain refuses to load an assembly[^],
Create WPF Application that uses Reloadable Plugins...[^].

Some of the aspects I considered in the above posts are may be to complex for your purpose. Please don''t be afraid; you case could be more simple. Please read and analyze the techniques against your use cases.

Fill free the ask your follow-up question (please, after you at least look through those posts) — the problem is not too easy to understand but perhaps is not the hardest in design and implementation.

—SA


...编辑#3 ...

事实证明,利用.NET中已有的功能,它们可能是一种更简单的方法,请参阅:[
您可以在这里的答复中看到,有些人认为脚本"是指解释器,例如BASIC,LOGO,PostScript,而其他答复(例如我的)则假定您是指提供的已编译.exe.文件,并返回结果,或引起副作用,例如打印.

在语言中创建迷你语言的编程中有着悠久的迷恋"传统,其最现代的最新形式是流行语特定领域的语言".从Forth到Logo等.也许我们甚至可以将.NET支持的语言视为基础CIL/JIT,FrameWork,引擎上的面孔".

如果您的目的是针对特定行业,目标受众构建自己的风味语言(解释或编译),这没什么不对(例如LOGO,LISP上带有Turtle Graphics的友好面孔",是用于教育).

如果您进一步阐明自己的意图和抱负,我想您会得到更明确的答复.

...结束编辑#2 ...

我想指出的是,此处显示的脚本示例是您自己创建的,而不是C#.顺便说一下,用什么语言在"if"语句后加上分号?

因此,如果您想使用C#中的功能来编译程序集,然后在其中执行方法:
... edit #3 ...

Turns out their may be a simpler way, taking advantage of something already in .NET, please see:[^].

... end edit #3 ...

... edit #2 ...

In this case, your example ''script'' code is close enough to C#, that the ''cost'' of creating a mini-parser that would turn it into ''legal'' C# would not be that high, in my opinion. But, then, you have the issue of whether your intent is to have a finite/static tool that reliably handles a small sub-set of C#/.NET FrameWork functionality, or whether you intend for this specialty script language to be extensible, to allow defining and using new constructs built-up of what you provide the end-user.

You can see in your responses here that some folks are assuming that by ''script'' you mean an interpreter, like BASIC, LOGO, PostScript, and, other responses, like mine, have assumed you mean a compiled .exe that you feed a file, and get back results, or cause side-effects, like printing.

There is a long ''distaff'' tradition in programming of creating mini-languages within languages, whose most modern recent form is the buzzword "domain specific languages." From Forth to Logo, etc. Maybe we can even view the languages that .NET supports as being ''faces'' on the underlying CIL/JIT, FrameWork, engines.

Nothing wrong if your intent is to build your own flavour language, interpreted, or compiled, specific to an industry, a target audience (as LOGO, a ''friendly-face'' on LISP with Turtle Graphics, was for education).

If you clarify your intent, and your ambitions, further, I think you''ll get even clearer responses.

... end edit #2 ...

I''d like to point out that the example of your script shown here is your own creation, not C#. By the way, what language puts a semi-colon after the ''if'' statement ?

So, if you want to use the facilities in C# required to compile an assembly, and then execute methods in it:
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;

;您将必须首先编写一个解析器,以将脚本语言转换为有效的C#.

完成此操作后,您将找到许多有关如何编译和执行C#代码的示例,例如:[

...编辑#1 ...

另请参阅MS的如何使用C#编译器以编程方式编译代码",有关使用CSharpCodeProvider的详细演练[

是时候做些功课了:)

Bill,祝你好运

You are going to have to first write a parser to translate your script language into valid C#.

When you get that done, you can find numerous examples of how to compile and execute the C# code such as:[^]. There are also examples here on CP of how to execute "snippets" of text in C#.

... edit #1 ...

Also see MS''s "How to programmatically compile code using C# compiler," a detailed walk-through on using the CSharpCodeProvider[^].

... end edit #1 ...

Another strategy you might consider is to make your script language VB, and look into using the VB library in C#, specifically its ''Eval function.

Time for some homework :)

good luck, Bill


您可以使用编译器生成器.它相当复杂,但是功能强大.用C#编写的是 COCO/R ,但还有其他一些.
You can play with compiler generators. It''s quite complex, but powerful. One written in C# is COCO/R, but there are others out there.


这篇关于如何使用C#创建自己的脚本引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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