'Running scripts inside C#

I want to run javascript/Python/Ruby inside my application.

I have an application that creates process automatically based on user's definition. The process is generated in C#. I want to enable advanced users to inject script in predefined locations. Those scripts should be run from the C# process. For example, the created process will have some activities and in the middle the script should be run and then the process continues as before. Is there a mechanism to run those scripts from C#?



Solution 1:[1]

Look into IronPython and IronRuby -- these will allow you to easily interoperate with C#.

Solution 2:[2]

You can compile C# code from within a C# application using the CSharpCodeProvider class.

If the compile succeeds you can run the resulting assembly as returned via the CompiledAssembly property of the CompilerResults class.

Solution 3:[3]

Awesome C# scripting language - Script.Net

Solution 4:[4]

.NET has a scripting language including runtime engine in PowerShell which can be embedded in any .NET application.

Solution 5:[5]

You can compile C# code "on the fly" into an in-memory assembly. I think this is possible with IronPython and IronRuby as well. Look at the CodeDomProvider.CreateProvider method.

If you need to run scripts a lot, or if your process runs for a long time, you might want to load these assemblies into another AppDomain. And unload the AppDomain after you're done with the script. Otherwise you are unable to remove them from memory. This has some consequenses on the other classes in your project, because you have to marshall all calls.

Solution 6:[6]

Have you thought about Visual Studio for Applications? I haven't heard much about it since .NET 1.1, but it might be worth a look.

http://msdn.microsoft.com/en-us/library/ms974548.aspx

Solution 7:[7]

I've done exactly this just recently - allowed run-time addition of C# scripting.

It's not hard at all, and this article:

http://www.divil.co.uk/net/articles/plugins/scripting.asp

is a very useful summary of the details.

Solution 8:[8]

One of Microsoft's solutions to JavaScript in C# is ClearScript, which uses V8, Chrom browser's JavaScript engine. Check its short FAQtorial for code samples.

It has excellent two-way integration - iterator/enumerator, output parameters, optional parameters, parameter arrays, delegate, task/promise/async/await, bigint, and more.

Apart from that, I think the most distinguishing feature is that it does not depend on Rosyln or Dynamic Language Runtime. This can be good or bad - good because there may be a lot less dependencies (depending on your project's target), bad because you need to bundle the native, platform-dependent V8 dll.

If that is ok, you get to enjoy cutting edge JavaScript / ECMAScript. Everything you get on Chrome, or 98% ES6 as of 2022 Feb, plus several extensions. Speed is as fast as Chrome, obviously, so you get the best of both Google and Microsoft.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Serafina Brocious
Solution 2 jussij
Solution 3 kzotin
Solution 4 Steven Murawski
Solution 5 GvS
Solution 6 AngryHacker
Solution 7 Will Dean
Solution 8 Sheepy