动态编译的应用(2)

原创 2005年03月01日 16:54:00

前两天见到此文: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();
  }
 }
}
(未完待续)

相关文章推荐

联合使用 CSS、JavaScript 和 JSF 精心打造 Ajax 应用程序,第 2 部分: 动态 JSF 表单

原文地址: http://www.ibm.com/developerworks/cn/web/wa-aj-jsfcss2/ 处理事件和更新用户界面 很多 JSF HTML 组件都拥有 Java...

合使用 CSS、JavaScript 和 JSF 精心打造 Ajax 应用程序,第 2 部分: 动态 JSF 表单

合使用 CSS、JavaScript 和 JSF 精心打造 Ajax 应用程序,第 2 部分: 动态 JSF 表单 探索标准 JSF 组件中的 CSS 支持 Andrei Cioroian...

android Spinner(下拉菜单)的应用举例2---动态实现

AppMain.java package lxy.litsoft; import java.util.ArrayList; import java.util.List; import andro...

struts2 action的动态应用(DMI)

action DMI动态应用 创建action 类 package com.demo.action; import com.opensymphony.xwork2.ActionSuppor...

Struts2为应用指定多个配置文件和动态方法调用以及通配符的使用方法

在开发中一般不会说将所有的配置都放在struts.xml这个一个配置文件中去,因为这样会使得整个配置文件臃肿不堪无法维护,一般来说会进行分模块的进行配置文件的编写。下面来演示一下分模块来进行配置 其...

Struts2中应用JQuery插件实现动态(Tree)结构

转自:http://wangfeizy.iteye.com/blog/1184085 Struts2中使用JQuery插件实现动态树型(Tree)结构最 近在给学校做公寓管理系统。使用部门要求选择宿舍...
  • jiyanle
  • jiyanle
  • 2015年04月23日 21:48
  • 675

struts2拦截器的方法过滤功能在Action动态方法调用中的应用

拦截器是指动态拦截Action调用的对象,在执行Action中的方法之前,系统会首先执行struts.xml引用的拦截器,它能够在Action执行前后做一些相应的工作。但是,有些时候我们并不需要拦截器...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:动态编译的应用(2)
举报原因:
原因补充:

(最多只允许输入30个字)