Created
March 11, 2013 04:00
-
-
Save zhuqling/5131815 to your computer and use it in GitHub Desktop.
使用FLEE库计算表达式的值
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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