祝紫山(大可山人)博客[GDI+,WPF, .Net图形图像]

WPF,WinForms,asp.net开发,图形图像处理系统研究[信奉:凡事靠自己] QQ:110-678-9933

动态编译的应用(2)

前两天见到此文:http://dev.csdn.net/develop/article/57/57990.shtm
于是自己试着写了一段自动编译的东东:(Eval.cs)
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;

namespace CompilerRuntime
{
 /// <summary>
 /// Summary description for Eval.
 /// </summary>
 public class Eval
 {
  private string _evalExpression;
  public Eval()
  {
   //
   // TODO: Add constructor logic here
   //
  }

  public Eval(string evalExpression)
  {
   EvalExpression = evalExpression;
  }

  /// <summary>
  /// Eval Expression Property.
  /// </summary>
  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.CompileAssemblyFromDom(cp,unit).CompiledAssembly;
   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 oo = mi.Invoke(obj,null);
   return oo;
  }
 }
}

调试文件:(Form1.cs)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using CompilerRuntime;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Reflection;

namespace CompilerRuntime
{
 /// <summary>
 /// Summary description for Form1.
 /// </summary>
 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.
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();

   //
   // TODO: Add any constructor code after InitializeComponent call
   //
  }

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  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);
   this.txtBoxExpression.TabIndex = 0;
   this.txtBoxExpression.Text = "(3+200)-5";
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(0, 8);
   this.label1.Name = "label1";
   this.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 = "計算表達式";
   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 = "計算XML價格";
   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

  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [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.ToString();
  }

  private void btnEvalXmlPrice_Click(object sender, System.EventArgs e)
  {
   XmlDocument doc = new XmlDocument();
            doc.Load(@"../../price.xml");
   string 單價 = doc.SelectSingleNode("/價格表/商品[@名稱='筆記本']/單價").InnerText;
   string 數量 = doc.SelectSingleNode("/價格表/商品[@名稱='筆記本']/數量").InnerText;
   string 折扣 = doc.SelectSingleNode("/價格表/商品[@名稱='筆記本']/折扣").InnerText;
   string 其他優惠 = doc.SelectSingleNode("/價格表/商品[@名稱='筆記本']/其他優惠").InnerText;
   string 計算表達式 = doc.SelectSingleNode("/價格表/商品[@名稱='筆記本']/計算表達式").InnerText;

   計算表達式 = 計算表達式.Replace("單價",單價);
   計算表達式 = 計算表達式.Replace("數量",數量);
   計算表達式 = 計算表達式.Replace("折扣",折扣);
   計算表達式 = 計算表達式.Replace("其他優惠",其他優惠);

   Eval ev = new Eval();
   object o = ev.GetValue( 計算表達式 );
   txtBoxResult.Text = o.ToString();
  }
 }
}
(未完待续)

阅读更多
个人分类: C#类 XML技术 asp.net
想对作者说点什么? 我来说一句

易语言5.11动静态结合版.7z

2011年03月18日 934KB 下载

c#动态编译生成

2015年10月07日 43KB 下载

Java6动态编译案例

2010年08月23日 10KB 下载

动态编译java

2011年09月13日 12KB 下载

Apachev1.3.29forLinux

2010年05月17日 2.32MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭