private void Compiler(string code)
{
CompilerParameters vCompilerParameters = new CompilerParameters();
vCompilerParameters.GenerateExecutable = false;
vCompilerParameters.GenerateInMemory = true;
vCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
string vSource =
"using System.Windows.Forms;\n" +
"public class Temp\n" +
"{\n" +
" public void Test()\n" +
" {\n" +
" " + code + "\n" +
" }\n" +
"}\n";
CompilerResults vCompilerResults =
CodeDomProvider.CreateProvider("CSharp").CompileAssemblyFromSource(vCompilerParameters, vSource);
Assembly vAssembly = vCompilerResults.CompiledAssembly;
object vTemp = vAssembly.CreateInstance("Temp");
MethodInfo vTest = vTemp.GetType().GetMethod("Test");
vTest.Invoke(vTemp, null);
}
string code = "int sum = 0;\nfor(int i = 0; i < 100; i++)\n{\nsum += i;\n}\nMessageBox.Show(sum.ToString());";
Compiler(code);