Hi all.
I'm posting this in relation to
this thread. It's intended to be just a quick intro to using C# as a scripting language instead of a compiled language.
Due to the fact that CLR compiled applications must be accompanied by a compiler, .net languages provide a unique opportunity to compile code on-the-fly. In this case, we're using a compiled executable to call the compiler to compile code we provide at the moment we wish to execute it via Microsoft.CSharp.CSharpCodeProvider(), thus simulating the behavior of a scripting language. There are two engines I've found to accomplish this task:
- C# Script for .NET 2.0 is the engine I eventually chose. It is extremely small and relatively simple, therefore is also a cinch to modify.
- CS_Script is the bulkier, yet more feature full of the two, this one provides for caching of the compiled script, and for embedding the "interpreter" itself into any .net application.
I chose the former both because it was far easier to modify, and because it runs happier on
Mono than the alternative. Attached is a console and GUI build of my slightly modified version (scripts written for the console misbehave if run from the GUI interpreter). The changes include:
- The System, System.IO, System.Text, System.Collections.Generic, and System.Windows.Forms namespaces are included by default, and the necessary assemblies are referenced.
- The full path of the executing script will be provided as the first argument on the command line.
- The code generated from the scripts will be optimized and will not contain any debug information.
- The interpreter will compile code that does not contain a Main(). However, if you provide a Main(), you must also provide an enclosing class { } , and optionally, a namespace { }. This provides for multiple classes or namespaces in a single script.
I hope this info will be of use to somebody. As always, feedback is much appreciated
Ehtyar.