Original: http://ms.mblogger.cn/minBear/posts/2530.aspx
C # original code dynamic compilation function implementation method 2 ~
Using microsoft.csharp; using system.codedom.compiler;
CsharpcodeProvider _scp = new csharpcodeProvider (); CompilerParameters_cp = new compilerparameters ();
_Cp.generateexecutable = false; _cp.mainclass = "codecompile.runexpression";
_Cp.outputassembly = "runexpression.dll";
_Cp.includedebuginformation = true;
_Cp.referencedassemblies.add ("system.dll");
_Cp.generateinMemory = false;
_CP.WARNINGLEVEL = 3;
_CP.TREATWARNINGSSERRORS = false; _CP.CompilerOptions = "/ Optimize"; _cp.tempfiles = new tempfilecollection ("..", true);
_SCP.CREATECOMPILER (). CompileAsessemblyFromSource (_CP, this.txtcodecontent.text);
// Note: this.txtCodecontent.text is the original code content, my test original code is as follows:
Using system; using system.io;
Namespace CodeCompile {///
A summary description of /// runexpression. ///
Public class runexpression {public runexpression () {// // Todo: Add constructor logic //}
Public void Run () {INT i = 0; INT J = 1;
INT m = i j;
System.io.StreamWriter_fs = new system.io.streamwriter (@ "E: /1.txt", true); _fs.write (system.datetime.now.tostring ("YYYY-MM-DD MM: SS") _Fs.close ();}}}