针对前一篇转载的写了个小程序,测试一下。从执行速度上来说使用反射要比用数据结构堆栈的方式效率要低很多。
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;
}
}
}