C# Can a comparison in a String value return a Boolean value. eg. quot;5 lt; 10quot; return true(C# 字符串值中的比较能否返回布尔值.例如.lt;5lt;10返回真)
问题描述
有没有办法在字符串值中进行比较可以返回 Boolean
值.例子.If (5 > 5000) 显然会返回一个错误值.但我想做的是让5 > 5000"返回一个错误值.
Is there a way a comparison in a string value can return a Boolean
value. Example.
If (5 > 5000) would obviously return a false value. But what i wanted to do is have the "5 > 5000" return a false value.
示例.
string com = "5 > 10";
所以有没有办法让这个 com
变量返回一个 false
值,就好像它是整数之间的比较一样.
so is there a way to make this com
variable return a false
value as if it was a comparison between integers.
推荐答案
没有内置方法,但是 NCalc可以在这里帮忙
No built-in way but NCalc can help here
NCalc.Expression expr = new NCalc.Expression("5>10");
bool b = (bool)expr.Evaluate();
你甚至可以使用参数
NCalc.Expression expr = new NCalc.Expression("a<b");
expr.EvaluateParameter += (name, args) =>
{
if (name == "a") args.Result = 5;
if (name == "b") args.Result = 10;
};
bool b = (bool)expr.Evaluate();
这篇关于C# 字符串值中的比较能否返回布尔值.例如.<5<10"返回真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!