Code
sing System;
using System.Text;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;
/// <summary>
///Evaluator 的摘要说明
/// </summary>
///
namespace ItDeer.Public
{
public class Evaluator
{
object _compiled = null;
private string m_formula;
/// <summary>
/// 计算公式
/// </summary>
public string Formula
{
get
{
return m_formula;
}
set
{
m_formula = value;
}
}
public Evaluator()
{
}
public Evaluator(string formula)
{
Formula = formula;
}
public Double Execute()
{
if (Formula == null || Formula == "")
{
throw new Exception("请先设置Formula属性!");
}
return this.Execute(Formula);
}
public Double Execute(string formula)
{
constructEvaluator(formula);
MethodInfo m = _compiled.GetType().GetMethod("GetValue");
return (Double)m.Invoke(_compiled, null);
}
private void constructEvaluator(string formula)
{
ICodeCompiler compiler = (new CSharpCodeProvider().CreateCompiler());
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("system.dll");
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
StringBuilder str = new StringBuilder();
str.Append("using System; \n");
str.Append("namespace Stoway { \n");
str.Append("public class Formula { \n");
str.AppendFormat(" public {0} GetValue()", "Double");
str.Append("{");
str.AppendFormat(" return Convert.ToDouble({0}); ", formula);
str.Append("}\n");
str.Append("}\n");
str.Append("}");
CompilerResults cr = compiler.CompileAssemblyFromSource(cp, str.ToString());
if (cr.Errors.HasErrors)
{
throw new Exception("不是正确的表达式");
}
Assembly a = cr.CompiledAssembly;
_compiled = a.CreateInstance("Stoway.Formula");
}
public static Double GetValue(string formula)
{
return new Evaluator().Execute(formula);
}
}
}
sing System;
using System.Text;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;
/// <summary>
///Evaluator 的摘要说明
/// </summary>
///
namespace ItDeer.Public
{
public class Evaluator
{
object _compiled = null;
private string m_formula;
/// <summary>
/// 计算公式
/// </summary>
public string Formula
{
get
{
return m_formula;
}
set
{
m_formula = value;
}
}
public Evaluator()
{
}
public Evaluator(string formula)
{
Formula = formula;
}
public Double Execute()
{
if (Formula == null || Formula == "")
{
throw new Exception("请先设置Formula属性!");
}
return this.Execute(Formula);
}
public Double Execute(string formula)
{
constructEvaluator(formula);
MethodInfo m = _compiled.GetType().GetMethod("GetValue");
return (Double)m.Invoke(_compiled, null);
}
private void constructEvaluator(string formula)
{
ICodeCompiler compiler = (new CSharpCodeProvider().CreateCompiler());
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("system.dll");
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
StringBuilder str = new StringBuilder();
str.Append("using System; \n");
str.Append("namespace Stoway { \n");
str.Append("public class Formula { \n");
str.AppendFormat(" public {0} GetValue()", "Double");
str.Append("{");
str.AppendFormat(" return Convert.ToDouble({0}); ", formula);
str.Append("}\n");
str.Append("}\n");
str.Append("}");
CompilerResults cr = compiler.CompileAssemblyFromSource(cp, str.ToString());
if (cr.Errors.HasErrors)
{
throw new Exception("不是正确的表达式");
}
Assembly a = cr.CompiledAssembly;
_compiled = a.CreateInstance("Stoway.Formula");
}
public static Double GetValue(string formula)
{
return new Evaluator().Execute(formula);
}
}
}