我想评估字符串表达式并将结果转换为bool。
例如
string expression = !((71 > 70) && (80 > 71)) || 72 < 71 ;
上面的表达式必须求值并返回true。
谁能建议如何评估表达式并返回布尔值?
我不相信有任何内置的表达式求解器可用于这种表达式评估。我建议使用Microsoft.CodeAnalysis.CSharp.Scripting进行评估。
string expression = "!((71 > 70) && (80 > 71)) || 72 < 71";
bool output = CSharpScript.EvaluateAsync<bool>(expression).Result;
// -> false
您的问题
!((71 > 70) && (80 > 71)) || 72 < 71
!((true) && (true)) || false
!(true & true)
false
string express = "within quotes";
。以上(string expression = !((71 > 70) && (80 > 71)) || 72 < 71 ;
)中的语句不是有效的C#语句。本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句