-1

I have a C# program that uses objects from a class that has a method looking something like this:

public double TheMethod(double argument1, double argument2, ...,double argumentN)
{ 
    //method body: do something with the arguments
    //return the result
}

I would like to give the user the ability to write their own logic into the method body and then have that be used when the program is run.

It seems to me that scripting may be the way to go, but I am having trouble trying to get started. What is the best way to approach this situation?

Note: If it matters, the program would be run from within a larger main desktop application. I would like the user to be able to write their code when the application is already running through some sort of editor program. Their code would then be saved and used once they launch the program that actually uses the method.

user57029
  • 78
  • 5
  • There are already plenty of questions and article on the topic - https://www.bing.com/search?q=c%23+scripting (or any other search engine you like Yahoo, Yandex,... as presumably Google was not enough). "trouble trying to get {scripting} started" is very poor explanation of problem you hit implementing one of many proposals - if you have particular problem setting up scripting it is better to ask that instead. – Alexei Levenkov Dec 16 '16 at 06:30

2 Answers2

2

You can certainly dynamically compile and execute C#. See Is it possible to dynamically compile and execute C# code fragments?

Depending on who your users are and how their script needs to integrate into the wider application, IronPython might be a nicer option - essentially scripting in python. See https://blogs.msdn.microsoft.com/charlie/2009/10/25/running-ironpython-scripts-from-a-c-4-0-program/ for details on integrating C# and IronPython.

As always when running any dynamically injected code from an external source, think about security.

Community
  • 1
  • 1
user783836
  • 3,099
  • 2
  • 29
  • 34
  • The IronPython suggestion looks promising. Do you know how this will affect the speed? For instance, if I write the same method body logic in IronPython as in the source code, will the IronPython implementation be significantly slower? – user57029 Dec 16 '16 at 06:26
  • With any dynamically compiled code you're going to incur some extra time when the code is compiled, both for C# and IronPython. http://stackoverflow.com/questions/3081661/speed-of-code-execution-ironpython-vs-c would indicate IronPython is slower, but it probably depends on the situation. How much code are your user's injecting? If the execution time is 5sec in C# and 7sec in IronPython, does that 2sec diff matter? Without context it's hard to tell. – user783836 Dec 16 '16 at 06:41
1

If you want your users to write in c#, the simplest thing would probably be to swap out the file (or at least the body of the method) with different copies and recompile the sub-application before each run.

It is possible to do a live replace of a method body during run-time, but it's not for the light hearted! https://msdn.microsoft.com/en-us/library/system.reflection.emit.aspx

If you are happy to do the work to create your own mini scripting language, take a look at Antlr http://www.antlr.org/

Stephan
  • 591
  • 4
  • 7
  • Reflection.Emit is totally different story and would be useful for building compiler... – Alexei Levenkov Dec 16 '16 at 06:33
  • @AlexeiLevenkov Care to explain your statement? Following his link the documentation reads: "The primary clients of these classes are **script engines** and compilers." Is it incorrect? – Skyqula Dec 16 '16 at 07:27