C # dynamic compilation code implementation

Source: Internet
Author: User

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using system. codedom. compiler; <br/> using Microsoft. CSHARP; <br/> using system. web; <br/> using system. reflection; <br/> namespace vsonline. framework <br/> {<br/> /// <summary> <br/> // code execution class <br/> /// </Summary> <br/> public class coderun <br/>{< br/> /// <summary> <br/> // Framework version, you can select V2.0, V3.0, v3.5 <br/> /// </Summary> <br/> private string compilerversion {Get; set ;} <br/> // <summary> <br/> // constructor <br/> /// </Summary> <br/> // <Param name = "compilerversion"> Framework version, you can select V2.0, V3.0, v3.5 </param> <br/> Public coderun (string compilerversion) <br/>{< br/> compilerversion = compilerversion; <br/>}< br/> /// <summary> <br/> // constructor, the default version is 3.5. <br/> /// </Summary> <br/> Public coderun () <br/>{< br/> compilerversion = "v3.5 "; <br/>}< br/> /// <summary> <br/> // dynamically compile and execute the Code <br/> /// </Summary> <br />/// <Param name = "code"> Code </param> <br/> /// <returns> return output content </returns> <br/> Public list <string> Run (string code, string ID) <br/>{< br/> Fiddler. outputs. add (ID, new ler Ler (); <br/> compilerparameters compilerparams = new compilerparameters (); <br/> // compiler option Settings <br/> compilerparams. compileroptions = "/Target: Library/optimize"; <br/> // compilerparams. compileroptions + = @ "/lib:" C:/program files/reference assemblies/Microsoft/framework/v3.5 /"""; <br/> // output in memory during compilation <br/> compilerparams. generateinmemory = true; <br/> // generate debugging information <br/> compilerparams. includedebuginformation = false; <br/> // Add reference <br/> // foreach (string assembly in assemblies) <br/> // {<br/> // compilerparams. referencedassemblies. add (assembly); <br/>/}< br/> // compilerparams. referencedassemblies. add ("mscorlib. DLL "); <br/> // compilerparams. referencedassemblies. add ("system. DLL "); <br/> // If (this. compilerversion = "v3.5") <br/> // {<br/> // compilerparams. referencedassemblies. add ("system. core. DLL "); <br/>/}< br/> compilerparams. referencedassemblies. add ("mscorlib. DLL "); <br/> compilerparams. referencedassemblies. add ("system. DLL "); <br/> // compilerparams. referencedassemblies. add ("system. core. DLL "); <br/> string Path =" "; <br/> try <br/>{< br/> // system. web. httpcontext. current. request. physicalapplicationpath + ("/bin/webset. XML "); <br/> Path = httpcontext. current. request. physicalapplicationpath + ("/bin/"); </P> <p >}< br/> catch {}< br/> compilerparams. referencedassemblies. add (path + "vsonline. framework. DLL "); <br/> csharpcodeprovider compiler = new csharpcodeprovider (new dictionary <string, string> () {" compilerversion ", compilerversion }}); <br/> // compile <br/> code = code. replace ("console. writeline ", String. format ("vsonline. framework. fiddler. outputs [/"{0}/"]. writeline ", ID); <br/> code = code. replace ("console. write ", String. format ("vsonline. framework. fiddler. outputs [/"{0}/"]. write ", ID); <br/> compilerresults Results = compiler. compileassemblyfromsource (compilerparams, Code); <br/> // error <br/> If (results. errors. haserrors) <br/>{< br/> foreach (compilererror error in results. errors) <br/>{< br/> Fiddler. outputs [ID]. output. add (error. errortext + "/N"); <br/>}< br/> return returnoutput (ID ); <br/>}< br/> // create an Assembly <br/> Assembly ASM = results. compiledassembly; <br/> // obtain the compiled type <br/> Object mainclass = ASM. createinstance ("program"); <br/> type mainclasstype = mainclass. getType (); <br/> // output result <br/> mainclasstype. getmethod ("Main "). invoke (mainclass, null); <br/> return returnoutput (ID); <br/>}< br/> private list <string> returnoutput (string ID) <br/>{< br/> string [] Output = new string [Fiddler. outputs [ID]. output. count]; <br/> Fiddler. outputs [ID]. output. copyto (output, 0); <br/> Fiddler. outputs. remove (ID); <br/> return output. tolist (); <br/>}< br/> 

 

 

Public class blprogram <br/>{< br/> protected const string process_method_code = @ "<br/> using system; <br/> public class Program <br/>{{ <br/> Public static void main () <br/>{{ <br/> string parameter = "" {0} "; <br/> string result =" "{1 }""; <br/> detection (parameter, result); <br/>}< br/> static Void detection (string testparameters, string testresults) <br/>{{ <br/> console. writeline (testparameters + ""-"" + testresults); <br/>}< br/> }}"; <br/> /// <summary> <br/> // determine whether the program question is correct <br/> /// </Summary> <br/> /// <Param name = "strcode"> </param> <br/> // <returns> </returns> <br/> Public bool iscprogramright (string strcode) <br/>{< br/> string id = guid. newguid (). tostring (); <br/> List <string> output = new coderun (). run (strcode, ID); <br/> string stroutpt = ""; <br/> for (INT I = 0; I <output. count; I ++) <br/>{< br/> stroutpt + = output [I]; <br/>}< br/> return (stroutpt = "/N "); <br/>}< br/> /// <summary> <br/> // output the execution result <br/> /// </Summary> <br/> /// <Param name = "strcode"> </param> <br/> // <returns> </returns> <br/> Public String getoutput (string strcode) <br/>{< br/> string id = guid. newguid (). tostring (); <br/> List <string> output = new coderun (). run (strcode, ID); <br/> string stroutpt = ""; <br/> for (INT I = 0; I <output. count; I ++) <br/>{< br/> stroutpt + = output [I]; <br/>}< br/> return stroutpt; <br/>}< br/> /// <summary> <br/> // generate a Program <br/> /// </Summary> <br/>/ // <Param name = "parameter"> </param> <br/> // <Param name = "result"> </param> <br/> /// <returns> </returns> <br/> Public String getprogramstyle (string parameter, string result) <br/>{< br/> stringbuilder processmethod = new stringbuilder (); <br/> processmethod. appendformat (process_method_code, parameter, result); <br/> return processmethod. tostring (); <br/>}< br/> /// <summary> <br/> // determines whether the program is successfully generated. <br/> /// </Summary> <br/> // <Param name = "strcode"> </param> <br/> // <returns> </returns> <br/> Public bool bianyi (string strcode) <br/>{< br/> string id = guid. newguid (). tostring (); <br/> List <string> output = new coderun (). run (strcode, ID); <br/> string stroutpt = ""; <br/> for (INT I = 0; I <output. count; I ++) <br/>{< br/> stroutpt + = output [I]; <br/>}< br/> return (stroutpt = "/N" | stroutpt = "/N "); <br/>}< br/>} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.