what is the simplest and fastest way to support simple scripting in a .Net application? I search a lot but only find many things with practical no documentation or outdated since years. I only need to transfer a simple .Net Object to the script and get a bool result from it.
|
closed as too broad by gnat, MichaelT, GlenH7, Corbin March, TZHX Sep 13 at 15:00
There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.
It depends on your precise needs. Two ways I can think of:
| |||||
|
I think the simplest way would be to use one of the .Net scripting languages that already exist, like IronPython or IronRuby. For example, with IronPython you can write your script code in Python, execute it from your C# application and then process the results from the script. The code could look like this:
Another option would be to use the scripting capabilities of Roslyn, but that's not production ready yet. | ||||
|
Another scripting language you should consider is PowerShell. There's a fully working example of an application that exposes some of its internal objects to a script environment here. This is the description of the application from the book (emphasis mine):
| |||
|
You could build a temp class and invoke a method on that
But you really have to trust people who write the scripts. Just add parameters to the Run method to pass your object. | |||
|
Fastest and simplest? I really hope this isn't that, but as a really last resort, you can use the .NET Emit method to create hand crafted CIL code that you generate dynamically in your program. If your scripting requirement is really trivial, then this is probably a month or two's work. You can work out the CIL instruction set from using a reflector to look at how the compiler translates your C# code into CIL, and then 'borrow' these code fragments. | |||||||||||||||||
|