Dynamic compilation (2)

xiaoxiao2021-03-06  42

See this two days: http: //dev.9cbs.net/develop/Article/57/57990.shtm then tried to write a period of automatic compilation: (Eval.cs) Using System; use system .Codedom; using system.codedom.compiler; using system.reflection; using microsoft.csharp;

Namespace compilerRuntime {///

/// summary description for eval. /// public class evAl {private string_evaleXpression; public evAl () {// // Todo: add constructor logic here //}

Public eval (string evalexpression) {evaleXpression = evaleXpression;

///

/// al public string evaleXpression {get {return_evaleXpression;} set {_evalexpression = value;}}

public object GetValue (string value) {string codeToCompile = @ "using System; namespace Johnsun.VCSharp.Com {public class Eval {public Eval () {} public object GetValue () {return" value @ ";}}} "Return CreateCompilerParameters (CODETOCompile," Johnsun.vcsharp.com.eval "," EVAL.DLL ");}

public object CreateCompilerParameters (string codeSnippet, string compileMainClass, string compileOutputAssembly) {CodeSnippetCompileUnit unit = new CodeSnippetCompileUnit (codeSnippet); ICodeCompiler compiler = new CSharpCodeProvider () CreateCompiler ();. CompilerParameters cp = new CompilerParameters (); cp.GenerateExecutable = false; cp .MainClass = compileMainClass; cp.OutputAssembly = compileOutputAssembly; cp.IncludeDebugInformation = true; cp.ReferencedAssemblies.Add ( "System.dll"); cp.GenerateInMemory = true; cp.WarningLevel = 3; cp.TreatWarningsAsErrors = false; // . Sets a command line compiler arguments string //cp.CompilerOptions = "/ optimize";. // Sets a temporary files collection This TempFileCollection stores the temporary files // generated during a build in the current directory, and does not delete them after Compilation. //cp.tempfiles = new tempFileCollection ("..", true); assembly asm = compiler.compileassemblyFrom (CP, Unit) .Compile dAssembly; Type type = asm.GetType ( "Johnsun.ArmItAge.Com.Eval"); MethodInfo mi = type.GetMethod ( "GetValue", BindingFlags.Public | BindingFlags.Instance); object obj = asm.CreateInstance ( "Johnsun. Vcsharp.com.eval "); object = mi.invoke (obj, null); returno}}} debug file: (form1.cs) Using system; use system.drawing; useing system.collections; using system. Componentmodel; using system.windows.forms; using system.data; using system.xml; using system.xml.xpath; using system.reflection;

namespace CompilerRuntime {///

/// Summary description for Form1 /// public class Form1: System.Windows.Forms.Form {private System.Windows.Forms.Label label1; private System.Windows. .Forms.Label label2; private System.Windows.Forms.TextBox txtBoxResult; private System.Windows.Forms.Button btnEval; private System.Windows.Forms.TextBox txtBoxExpression; private System.Windows.Forms.Button btnEvalXmlPrice; /// < summary> /// Required designer variable /// private System.ComponentModel.Container components = null;. public Form1 () {// // Required for Windows Form Designer support // InitializeComponent ();

// // Todo: add any constructor code after initializationcomponent call //}

///

/// clean up any resources being used. /// protected Override Void Dispose (Bool Disposing) {if (disponents! = null) {components.dispose () }} Base.Dispose (Disposing);

#region Windows Form Designer generated code ///

/// Required method for Designer support - do not modify /// the contents of this method with the code editor /// private void InitializeComponent (). {this.txtBoxExpression = new System.Windows.Forms.TextBox (); this.label1 = new System.Windows.Forms.Label (); this.label2 = new System.Windows.Forms.Label (); this.txtBoxResult = New system.windows.Forms.TextBox (); this.btneval = new system.windows.Forms.Button (); this.btnevalXMLPRICE = new system.windows.Forms.Button (); this.suspendlayout (); // / / txtBoxExpression // this.txtBoxExpression.Location = new System.Drawing.Point (64, 8); this.txtBoxExpression.Name = "txtBoxExpression"; this.txtBoxExpression.Size = new System.Drawing.Size (264, 20); THISTXTBOXEXPRESSION.TABINDEX = 0; this.txtBoxExpression.text = "(3 200) -5"; /// Label1 // this.label1.location = new system.drawing.point (0, 8); this .Label1.name = "label1"; th Is.label1.size = new system.drawing.size (64, 16); this.label1.tabindex = 1; this.label1.text = "expression:"; // // label2 // this.label2.location = New system.drawing.point (0, 40); this.label2.name = "label2"; this.label2.size = new system.drawing.size (56, 16); this.label2.tabindex = 2; this. Label2.text = "Result:"; // // txtBoxResult // this.txtBoxResult.location = new system.drawing.point (64, 40); this.txtBoxResult.name = "txtboxResult"; this.txtBoxResult.size = New system.drawing.size (264, 20); this.txtBoxResult.tabindex = 3;

THIS.TXTBOXRESULT.TEXT = ""; // // btneval // this.btneval.location = new system.drawing.point (48, 72); this.btneval.name = "btneval"; this.btneval.tabindex = 4; this.btneval.text = "Computational expression"; this.btneval.click = new system.eventhandler (this.btneval_click); // // btnevalXMLPRICE // this.btnevalXMLPRICE.LOCATION = new system.drawing.point (144, 72); this.btnEvalXmlPrice.Name = "btnEvalXmlPrice"; this.btnEvalXmlPrice.Size = new System.Drawing.Size (88, 23); this.btnEvalXmlPrice.TabIndex = 5; this.btnEvalXmlPrice.Text = "calculation XML prices "; this.btnEvalXmlPrice.Click = new System.EventHandler (this.btnEvalXmlPrice_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size (5, 13); this.ClientSize = new System .Drawing.size (344, 102); this.Controls.add (this.btnevalXmlprice); this.controls.add (this.btneval); this.controls.add (this.txtBoxResult); this.controls.add (this .Label2); this.controls.add (this.label1); this.controls.add (this .txtBoxExpression; this.name = "form1"; this.text = "form1"; this.ResumeLayout (false);} #ENDREGION

///

/// the main entry point for the application. /// [stathread] static void main () {Application.run (new form1 ());}

Private void btneval_click (object sender, system.eventargs e) {EVAL EV = new evAl (); object o = ev.getValue (txtBoxExpression.Text); txtBoxResult.text = o.to.tostring ()

Private void btnevalXMLPRICE_CLICK (Object Sender, System.EventArgs E) {xmldocument doc = new xmldocument (); doc.low (@ "../../ Price.xml"); String Unit Price = Doc.selectsinglenode ("/ Price Table / Products [@ name = 'notebook'] / unique price ") .INNNERTEXT; STRING quantity = doc.selectsinglenode (" / price list / product [@ name = 'notebook'] / quantity ") .INNNERTEXT; STRING Discount = DOC. Selectsinglenode ("/ Price Table / Products [@ Name = 'Notebook'] / Discount") .INnertext; String Other Offers = Doc.selectsinglenode ("/ Price Table / Products [@ Name = 'Notebook'] / Other Offers") .Innertext; string calculation expression = doc.selectsinglenode ("/ price list / commodity [@ name = 'notebook'] / calculation expression") .innertext; Computational expression = calculation expression.replace ("unit price", single price ); Calculation expression = calculation expression. Repelace ("quantity", quantity); calculation expressions = calculate expression. Repeed ("Discount", discount); calculation expression = calculated expression. Repeed ("Other Offers" ,Other offers);

Eval EV = new evAl (); Object O = ev.getValue; txtBoxResult.text = o.to.tostring ();}}}

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

New Post(0)