现在的位置: 首页 > 综合 > 正文

C#下的JS脚本引擎ReoScript

2018年05月27日 ⁄ 综合 ⁄ 共 1720字 ⁄ 字号 评论关闭

在上一篇博文中,我写了一篇关于开源的表格控件ReoGrid的文章,点这里

我大致研究了一下它的执行脚本那块的功能,发现它使用了一个叫ReoScript的脚本引擎,并且也是一个开源项目,点这里

02.png

通过上图,可以清晰的看到这个引擎的使用过程。

下面为集成到.net开发环境中的方法

Run your script

03.png
Prepare script, and run it by using ScriptRunningMachine:
08.png
Result:
07.png
There are some basic features provided by ReoScript core, such as Array.
10.png
Result:
09.png

Add your objects and functions into script

16.png

3 ways to add objects and functions

05.png

Proxy objects

Without modifying your existed objects, add proxy objects for .NET objects will make it available in script. There is a .NET class named 'Car' and its proxy class named 'CarProxy', the 'CarProxy' class only be used for script.

11.png

And there is a proxy class which provided for script:

12.png

Then run script:
13.png

Using ScriptVisible Attribute

Add 'ScriptVisible' attribute to class, field, property and method will make it visible to script.
14.png
this feature available since v1.4

DirectAccess mode (.NET Reflection)

No need to write anything, just enable the DirectAccess mode of ScriptRunningMachin.
15.png

Choose a way to integrate with your Application

Here are some tips that you can reference.

  • Proxy objects - Although proxy objects need to be written, but this mode has a good reliability and it is very safety. If you are planning to provide script available for your end-user then you may choose this.
  • ScriptVisible - There is a few difficult to control what objects and properties to be available in script. Consider that risk and choose this mode.
  • DirectAccess - This mode mix the world between .NET and Script, and script may become unavailable after you changed the appearance of .NET objects. So make sure that script available and choose this mode.

再下来就是怎么部署

Setup

1. Download ReoScript binary or build source file. Add the following DLLs into reference list of your project.

Antlr3.Runtime.dll
unvell.ReoScript.dll


2. Import the following namespace

using unvell.ReoScript;


3. Create ScriptRunningMachine and run script

ScriptRunningMachine srm = new ScriptRunningMachine();
srm.Run(...);    // source can be string, file and stream

抱歉!评论已关闭.