Skip to content

Instantly share code, notes, and snippets.

@zhuqling
Created March 11, 2013 04:00
Show Gist options
  • Save zhuqling/5131815 to your computer and use it in GitHub Desktop.
Save zhuqling/5131815 to your computer and use it in GitHub Desktop.
使用FLEE库计算表达式的值
string result = "";
try
{
// 按输入公式文本计算结果
ExpressionContext context = new ExpressionContext();
context.Imports.AddType(typeof (Math));
// context.Variables["a"] = 100; 可以在表达式内使用变量,再使用Variables传递变量值进行计算
IDynamicExpression eDynamic = context.CompileDynamic(calcMessage.Formula); // "sqrt(a) + pi"
result = eDynamic.Evaluate().ToString();
}
catch
{
result = "表达式不正确,无法计算";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment