[求助]如何将字符串表达式转换为C#的表达式?
比如有 "(50> 40 && 50 <30) || (90> 60) " 这样一个字符串,如何写一个函数,将它转换为 (50> 40 && 50 <30) || (90> 60) ,然后判断其是否是true,false?
[解决办法]
using System;
using System.Text;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;
namespace Obx.Common.Utilities
{
public class ObxEvaluator
{
object _compiled = null;
private string m_formula;
/// <summary>
/// Evaluate a numeric string
/// </summary>
public string Formula
{
get { return m_formula; }
set { m_formula = value; }
}
/// <summary>
///
/// </summary>
public ObxEvaluator() { }
/// <summary>
///
/// </summary>
/// <param name= "formula "> </param>
public ObxEvaluator(string formula)
{
Formula = formula;
}
/// <summary>
///
/// </summary>
/// <returns> </returns>
public Double Execute()
{
if (Formula == null || Formula == " ")
{
throw new Exception( "Please set formula first! ");
}
return this.Execute(Formula);
}
/// <summary>
///
/// </summary>
/// <returns> </returns>
public Boolean ExecuteBoolean()
{
if (Formula == null || Formula == " ")
{
throw new Exception( "Please set formula first! ");
}
constructEvaluator(Formula);
MethodInfo m = _compiled.GetType().GetMethod( "GetBooleanValue ");
return (Boolean)m.Invoke(_compiled, null);
}
//public DateTime ExecuteDateTime()
//{
// if (Formula == null || Formula == " ")
// {
// throw new Exception( "Please set formula first! ");
// }
// constructEvaluator(Formula);
// MethodInfo m = _compiled.GetType().GetMethod( "GetDateTimeValue ");
// return (DateTime)m.Invoke(_compiled, null);
//}
/// <summary>
///
/// </summary>
/// <param name= "formula "> </param>
/// <returns> </returns>
public Double Execute(string formula)
{
constructEvaluator(formula);
MethodInfo m = _compiled.GetType().GetMethod( "GetValue ");
return (Double)m.Invoke(_compiled, null);
}
/// <summary>
///
/// </summary>
/// <param name= "formula "> </param>
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.AppendFormat( " public {0} GetBooleanValue() ", "Boolean ");
str.Append( "{ ");
str.AppendFormat( " return Convert.ToBoolean({0}); ", formula);
str.Append( "}\n ");
str.AppendFormat( " public {0} GetDateTimeValue() ", "DateTime ");
str.Append( "{ ");
str.AppendFormat( " return Convert.ToDateTime({0}); ", formula);
str.Append( "}\n ");
str.Append( "}\n ");
str.Append( "} ");
CompilerResults cr = compiler.CompileAssemblyFromSource(cp, str.ToString());
if (cr.Errors.HasErrors)
{
throw new Exception(formula + " is not a right formula! ");
}
Assembly a = cr.CompiledAssembly;
_compiled = a.CreateInstance( "Stoway.Formula ");
}
}//public class ObxEvaluator
}//namespace Obx.Common.Utilities
上面的类编译好以后直接用里面的方法ExecuteBoolean(你的字符串)就行了
或者用MSScriptControl 可以直接执行脚本 不过没用过
[解决办法]
//先在项目中添加COM引用Microsoft Script Control 1.0
using MSScriptControl;
//...
ScriptControl vScriptControl = new ScriptControl();
vScriptControl.Language = "JavaScript ";
Text = vScriptControl.Eval( "(50> 40 && 50 <30) || (90> 60) ").ToString();