using CodeDOM I was able to compile the source code I had written in a separate file:
CompilerResults results = codeCompiler.CompileAssemblyFromFile(parameters, @"C:\MyScript.cs");
results.CompiledAssembly.GetType("Script").GetMethod("Main").Invoke(null, null);
Is there a way that instead of reading that source code from a file – MyScript.cs – so I can just put a rich text box on the form and place the code in that?
Look at the documentation of
CSharpCodeProvider: it contains a methodCompileAssemblyFromSource()that takes one or more strings that contain the code to compile.So, your code could look like: