利用反射機制計算字符表達式的測試。

針對前一篇轉載的寫了個小程序,測試一下。從執行速度上來說使用反射要比用數據結構堆棧的方式效率要低很多。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.CodeDom.Compiler;
using System.Reflection;
using System.IO;
using Microsoft.CSharp;

namespace ExpressionReflector
{
 /// <summary>
 /// Form1 的摘要說明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.TextBox txtExpression;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.TextBox txtResult;
  private System.Windows.Forms.Button btnCalc;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.ComboBox cbxOperator;
  /// <summary>
  /// 必需的設計器變量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗體設計器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
   //
   this.cbxOperator.SelectedIndex=0;
  }

  /// <summary>
  /// 清理所有正在使用的資源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗體設計器生成的代碼
  /// <summary>
  /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {
   this.txtExpression = new System.Windows.Forms.TextBox();
   this.txtResult = new System.Windows.Forms.TextBox();
   this.label1 = new System.Windows.Forms.Label();
   this.btnCalc = new System.Windows.Forms.Button();
   this.label2 = new System.Windows.Forms.Label();
   this.cbxOperator = new System.Windows.Forms.ComboBox();
   this.SuspendLayout();
   //
   // txtExpression
   //
   this.txtExpression.Location = new System.Drawing.Point(112, 40);
   this.txtExpression.Name = "txtExpression";
   this.txtExpression.Size = new System.Drawing.Size(136, 21);
   this.txtExpression.TabIndex = 0;
   this.txtExpression.Text = "";
   //
   // txtResult
   //
   this.txtResult.Location = new System.Drawing.Point(112, 72);
   this.txtResult.Name = "txtResult";
   this.txtResult.TabIndex = 1;
   this.txtResult.Text = "";
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(32, 40);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(56, 23);
   this.label1.TabIndex = 2;
   this.label1.Text = "表達式:";
   this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
   //
   // btnCalc
   //
   this.btnCalc.FlatStyle = System.Windows.Forms.FlatStyle.System;
   this.btnCalc.Location = new System.Drawing.Point(16, 72);
   this.btnCalc.Name = "btnCalc";
   this.btnCalc.TabIndex = 3;
   this.btnCalc.Text = "計算";
   this.btnCalc.Click += new System.EventHandler(this.btnCalc_Click);
   //
   // label2
   //
   this.label2.Location = new System.Drawing.Point(32, 8);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(64, 23);
   this.label2.TabIndex = 5;
   this.label2.Text = "變量類型";
   this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
   //
   // cbxOperator
   //
   this.cbxOperator.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
   this.cbxOperator.ItemHeight = 12;
   this.cbxOperator.Items.AddRange(new object[] {
                "int",
                "float",
                "double"});
   this.cbxOperator.Location = new System.Drawing.Point(112, 8);
   this.cbxOperator.Name = "cbxOperator";
   this.cbxOperator.Size = new System.Drawing.Size(72, 20);
   this.cbxOperator.TabIndex = 4;
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(264, 118);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.cbxOperator);
   this.Controls.Add(this.btnCalc);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.txtResult);
   this.Controls.Add(this.txtExpression);
   this.MaximizeBox = false;
   this.Name = "Form1";
   this.Text = "反射計算字符表達式";
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 應用程序的主入口點。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void btnCalc_Click(object sender, System.EventArgs e)
  {
   this.txtResult.Text = Eval(this.txtExpression.Text).ToString();;
  }

  private object Eval(string cCharpCode)
  {
   cCharpCode = this.cbxOperator.Text+" i="+cCharpCode+";";
   CSharpCodeProvider csharpCodeProvider = new CSharpCodeProvider();
   ICodeCompiler compiler = csharpCodeProvider.CreateCompiler();
   CompilerParameters cp = new CompilerParameters();
   cp.ReferencedAssemblies.Add("system.dll");
   cp.CompilerOptions = "/t:library";
   cp.GenerateInMemory = true;
   StringBuilder myCode = new StringBuilder();
   myCode.Append("using System;");
   myCode.Append("namespace CoustomEval{");
   myCode.Append("class myLib {private "+cCharpCode+" public "+this.cbxOperator.Text+" myMethod(){return i;}}");
   myCode.Append("}");
   CompilerResults cr = compiler.CompileAssemblyFromSource(cp,myCode.ToString());
   Assembly assembly = cr.CompiledAssembly;
   object tmp = assembly.CreateInstance("CoustomEval.myLib");
   Type type = tmp.GetType();
   MethodInfo mi = type.GetMethod("myMethod");
   object result = mi.Invoke(tmp,null);
   return result;
  }
 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章