Dynamic compilation

xiaoxiao2021-03-06  55

using System; using System.CodeDom; using System.CodeDom.Compiler; using Microsoft.CSharp; using System.Text; using System.IO; using System.Reflection; namespace CompiledDll {/ **

/// Summary description for class1 /// public class CompiledDll {public CompiledDll () {} public string outFileName;. public bool complie (string fileName) {ICodeCompiler comp = (new CSharpCodeProvider () CreateCompiler ().); CompilerParameters cp = new CompilerParameters (); Cp.referencedassemblies.add ("system.dll"); cp.referencedassemblies.add ("system.data.dll"); cp.referencedassemblies.add ("system.xml.dll"); cp.referenceDassembly. Add ("System.Windows.Forms.dll"); //cp.referenceDassemblies.add(@"d:projectsvs2003testbusinessdll.dll ");

Cp.GenerateExecutable = false; cp.generateInMemory = false; cp.outputassembly = outfilename; streamreader file = file.opentext (@filename); string code = file.readToeend (); file.close ();

CompilerResults cr = comp.compileAssemblyfromsource (CP, CODE);

IF (cr.erroilder error = new stringbuilder (); error.Append ("ERROR Compiling Expression:"); Foreach (CompileRerror Err IN CR.Error) {Error.Appendformat ("{0}", ERR . ErrText);} throw new exception ("Error Compiling Expression:" Error.Tostring ());} else {return true;}}}}

转载请注明原文地址:https://www.9cbs.com/read-114828.html

New Post(0)