选择C#脚本引擎 [英] Choosing a C# script engine

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

问题描述

我需要一个C#应用程序的脚本引擎,但无法弄清楚什么是最佳选择。我已经浏览了网络并找到了一些可能的候选人,但我认为我需要一些有C#脚本实践经验的人的建议。我正在寻找可以很容易地集成到C#应用程序中的东西,但功能强大到足以满足我的要求:



脚本语言应该是C#。 />


该应用程序具有许多用户可定义的业务规则,每个规则都有一个条件,用于确定规则的适用时间和方式。这个条件是C#脚本。



还会有库代码文件形式的C#脚本,用户可以在其中定义可用于的规则条件。业务规则和库代码文件存储在数据库中。



应该可以将主应用程序中的对象(方法,字段和属性)暴露给脚本引擎这样脚本就可以使用应用程序中的功能并与之通信。



加载后,会经常评估规则,因此脚本的评估应该很快。如果脚本代码被编译一次然后从缓存执行直到脚本被更改,那么最好的可能是。



最好对调试脚本提供一些支持。 />


我还需要一个可嵌入的C#编辑器,它具有语法高亮,代码完成和智能感。就像在Visual Studio中一样。我需要一些有实际经验的人给出一些建议,所以我不会进入一些讨厌的洞,不得不花费无数个小时与一些越野车附件挣扎。



谢谢!

解决方案

由于.net有一个CodeDOM编译器,因此很容易在运行时使用和编译代码片段。



唯一真正的问题是你的应用程序架构必须支持这种功能的某种插件或接口。

 < span class =code-keyword> public   class  pluginXYZ:IMyAppPlugin 
{
public object 运行(IMyAPI api, object [] args)
{
// 您的代码
}
}



所以从上面的例子中你可以看到你我创建了一个 IMyAppPlugin 你的代码片必须实现,而 IMyAPI 是你内部功能的接口说(查询() LogMessage()等等。)



那里很多商业和开源语法高亮和intellisense编辑器也可以使用:

用于语法突出显示的快速彩色TextBox [ ^ ]

http: //www.icsharpcode.net/OpenSource/SD/ [ ^ ]


I need a script engine for a C# app but can’t figure out what is the best choice. I have looked around the net and found a number of possible candidates, but I think I need some advice from someone with practical experience in C# scripting. I am looking for something that is fairly easy to integrate into the C# app, yet is powerful enough to satisfy my requirements:

The scripting language should be C#.

The app has a number of user definable "business rules ", each having a condition that determines when and how the rules apply. This condition is the C# script.

There will also be C# scripts in the form of "library code files" where the user can define common functions that can be used in the rule conditions. Business rules and library code files are stored in a database.

It should be possible to expose objects (methods, fields and properties) in the main app to the scripting engine so that the scripts can use functionality in the app and communicate with it.

Once loaded, the rules are evaluated frequently, so the evaluation of the scripts should be fast. Best is probably if script code is compiled once and then executed from a cache until the script is changed.

There should preferably be some support for debugging the scripts.

I also need an embeddable C# editor with syntax highlighting, code completion and intelli sense. Like in Visual Studio. I need some advice from someone with practical experience also on this choice, so I don’t step into some nasty hole and have to spend countless hours struggling with some buggy add-on.

Thanks!

解决方案

Since .net has a CodeDOM compiler it is very easy to work with and compile code snippets at runtime.

The only real issue is your application architecture must support some kind of plug-in or interface for such a feature.

public class pluginXYZ : IMyAppPlugin
{
     public object Run(IMyAPI api, object[] args)
     {
        // your code here
     }
}


So from the sample above you can see you have created a IMyAppPlugin which your snippets must implement and the IMyAPI is an interface to your internal functionality say (Query(), LogMessage() etc.)

There are a lot of commercial and open source syntax highlighting and intellisense editors also which you can use:
Fast Colored TextBox for Syntax Highlighting[^]
http://www.icsharpcode.net/OpenSource/SD/[^]


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

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