Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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!
Posted

1 solution

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.
C#
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/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900