... edit #3 ...
Turns out their may be a simpler way, taking advantage of something already in .NET, please see:[
^].
... end edit #3 ...
... edit #2 ...
In this case, your example 'script' code is close enough to C#, that the 'cost' of creating a mini-parser that would turn it into 'legal' C# would not be that high, in my opinion. But, then, you have the issue of whether your intent is to have a finite/static tool that reliably handles a small sub-set of C#/.NET FrameWork functionality, or whether you intend for this specialty script language to be extensible, to allow defining and using new constructs built-up of what you provide the end-user.
You can see in your responses here that some folks are assuming that by 'script' you mean an interpreter, like BASIC, LOGO, PostScript, and, other responses, like mine, have assumed you mean a compiled .exe that you feed a file, and get back results, or cause side-effects, like printing.
There is a long 'distaff' tradition in programming of creating mini-languages within languages, whose most modern recent form is the buzzword "domain specific languages." From Forth to Logo, etc. Maybe we can even view the languages that .NET supports as being 'faces' on the underlying CIL/JIT, FrameWork, engines.
Nothing wrong if your intent is to build your own flavour language, interpreted, or compiled, specific to an industry, a target audience (as LOGO, a 'friendly-face' on LISP with Turtle Graphics, was for education).
If you clarify your intent, and your ambitions, further, I think you'll get even clearer responses.
... end edit #2 ...
I'd like to point out that the example of your script shown here is your own creation, not C#. By the way, what language puts a semi-colon after the 'if' statement ?
So, if you want to use the facilities in C# required to compile an assembly, and then execute methods in it:
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;
You are going to have to first write a parser to translate your script language into valid C#.
When you get that done, you can find numerous examples of how to compile and execute the C# code such as:[
^]. There are also examples here on CP of how to execute "snippets" of text in C#.
... edit #1 ...
Also see MS's "How to programmatically compile code using C# compiler," a detailed walk-through on using the CSharpCodeProvider[
^].
... end edit #1 ...
Another strategy you might consider is to make your script language VB, and look into using the VB library in C#, specifically its 'Eval function.
Time for some homework :)
good luck, Bill