1

I am currently evaluating scripting options for my .NET application. The user should be able to write a script (in a dedicated text/code editor within the application) to control the application itself. The application is completely written in C#. As scripting languages I am currently evaluating

  • LUA
  • IronPython

I found the following article (https://www.cyotek.com/blog/adding-scripting-to-net-applications) which relies on JavaScript which could also be an option. Currently it is not clear to me how I can offer my internal objects used in the application and custom functions for the used script. The link above relies on JINT (Javascript Interpreter for .NET) which offers means to add own method and objects for the script:

  protected virtual void InitializeEnvironment()
  {
    this.AddFunction("print", new Action<object>(this.WriteLine));
    this.AddFunction("log", new Action<object>(this.WriteLine));
    this.AddFunction("cls", new Action(this.ClearScreen));

    // interactive functions
    this.AddFunction("alert", new Action<object>(this.ShowAlert));
    this.AddFunction("confirm", new Func<object, bool>(this.ShowConfirm));
    this.AddFunction("prompt", new Func<object, object, string>(this.ShowPrompt));
  }

I didn't find means to do this with IronPython - at least it is not mentioned in the documentation and there are no examples for this kind of approach. Perhaps I am completely on the wrong track here - any help/tip would be great.

4
  • 1
    Third option: script with C# using CSharpCodeProvider. Commented Nov 12, 2021 at 15:37
  • 1
    You can add Lua scripting with NLua Commented Nov 12, 2021 at 16:18
  • @Egor yes I know, but here I have the same problem. How can I add my own methods and objects... Commented Nov 12, 2021 at 17:33
  • 1
    RegisterTableFunction Commented Nov 12, 2021 at 19:41

1 Answer 1

1

For light-footprint usage IronPython may be good option. You can add custom functions in the execution scope:

Adding static method to IronPython scope

You might also want to check out AlterNET studio here:

https://www.alternetsoft.com/products/studio

It’s a commercial library that offers in-app scripting/debugging for the following languages/technologies:

-C#/Visual Basic, based on Roslyn

-TypeScript/JavaScript based on ClearScript and Google developers tools

IronPython

Python, based on Debug Adapter Protocol - this one is still in a development.

You may find a bit more information here:

https://www.alternetsoft.com/news/alternet-studio-7-0-highlights

https://www.alternetsoft.com/blog/python-script-debugger-based-on-debug-adapter-protocol

With all these options you can access objects defined in .NET application, but they have pros and cons when it comes to the script execution performance, in-app debugging, memory footprints, etc.

If you need scripting only, you would not need a commercial-grade solution, it may be of value though if you also need script debugging or code editing/writing functionality in your application.

Dmitry

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.