C# 字符串值中的比较能否返回布尔值.例如.<5<10"返回真 [英] C# Can a comparison in a String value return a Boolean value. eg. "5 < 10" return true

查看:31
本文介绍了C# 字符串值中的比较能否返回布尔值.例如.<5<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# 字符串值中的比较能否返回布尔值.例如.&lt;5&lt;10"返回真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆