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 {///
Public eval (string evalexpression) {evaleXpression = evaleXpression;
///
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 {///
// // Todo: add any constructor code after initializationcomponent call //}
///
#region Windows Form Designer generated code ///
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
///
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 ();}}}