Recently I saw a C # cainiao-level tool which is written in form.Code, Re-compile and execute in the console
At that time, I thought it was very novel. I just downloaded it and checked it out. I didn't expect it to be written in F # language. There were a few cainiao in functional languages --
In the spirit of curiosity, and in order to have something to talk about at the monthly meeting, I found some information on the Internet and wrote one by myself. The operation diagram is as follows:
OK,ProgramIt is very simple. A new form is created on the console, and the button event of form is to call the console method and re-compile the code for execution.
The following is the part of the concatenation code in the constructor.
/// <Summary> /// Parses string Constructor /// </Summary> /// <Param name = "items"> String Array to be parsed </Param> Private Void Constructevaluator (evaluatoritem [] items ){ // Create a C # compiler instance Icodecompiler comp = ( New Csharpcodeprovider (). createcompiler ()); // Compiler input parameters Compilerparameters CP = New Compilerparameters (); CP. referencedassemblies. Add ( " System. dll " ); // Add reference to assembly system. dll CP. referencedassemblies. Add ( " System. Data. dll " ); // Add reference to assembly system. Data. dll CP. referencedassemblies. Add ( " System. xml. dll " ); // Add reference to assembly system. xml. dll CP. generateexecutable = False ; // Do not generate executable files CP. generateinmemory = True ; // Run in memory Stringbuilder code = New Stringbuilder (); // Create Code string /* * Add common and required reference strings */ Code. append ( " Using system; \ n " ); Code. append ( " Using system. Data; \ n " ); // Code. append ("using system. Data. sqlclient; \ n "); // Code. append ("using system. Data. oledb; \ n "); // Code. append ("using system. xml; \ n "); Code. append ( " Namespace evalguy {\ n " ); // The namespace of the generated code is evalguy, which is the same as that of the Code. Code. append ( " Public class _ evaluator {\ n " ); // Generate the _ evaluator class, And all executable code is run in this class Foreach (Evaluatoritem itemIn Items) // Traverse every executable string {Code. appendformat ( " Public {0} {1 }() " , // Add and define public function code Item. returntype. Name, // The function return value is a type of return value defined in an executable string. Item. Name ); // The function name is the name of the execution string defined in the executable string. Code. append (" {\ N " ); // Add function start brackets String [] STRs = item. expression. Split ( ' ; ' ); Code. append ( " Object OBJ = \ "\"; \ n " ); Code. append ( " Try {\ n " ); Foreach ( String S In STRs) {code. append (S + " ; \ N " );} Code. append ( " } \ N catch {}\ n " ); Code. append ( " Return OBJ; " ); Code. append ( " } \ N " ); // Add function ending brackets } Code. append ( " }} " ); // Add end class and end namespace brackets // Returns the result of the compiler instance. Compilerresults Cr = Comp. compileassemblyfromsource (CP, code. tostring ()); Try {;} Catch {} If (Cr. errors. haserrors) // If any error occurs {Stringbuilder Error = New Stringbuilder (); // Create an error message string Error. append ( " An incorrect expression is compiled: " ); // Add error text Foreach (Compilererror err In Cr. Errors) // Traverse every compilation Error {Error. appendformat ( " {0} \ n " , Err. errortext ); // Added to the error text, with a line break after each error } MessageBox. Show ( " Compilation error: " + Error. tostring ()); Return ;} Assembly = Cr. compiledassembly; // Get the assembly of the compiler instance _ Compiled = A. createinstance ( " Evalguy. _ Evaluator " );// Find and declare the evalguy. _ evaluator instance through the Assembly }
The role of this toolArticleI wrote it clearly: it is useless... At least I haven't used it at work these two weeks.
Cainiao tool, demo