评估条件表达式

编程入门 行业动态 更新时间:2024-10-14 04:30:09
本文介绍了评估条件表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在这,我救了我的如果条件,数据库作为一个字符串的情况。例如:

I have a scenario in which I am saving my "if" conditions in database as a string. For example:

String condition = "(([age] >= 28) && ([nationality] == 'US'))";

String condition = "([age] >= 28)";

现在,我要评估该用户已输入条件的语法的正确。这些例子语法不正确的:

Now, I want to evaluate that the user has input the condition syntactically correct. These are example of incorrect syntax:

String condition = "(([age] >= 28) && ([nationality] == 'US')"; //Missed ')' bracket String condition = "[age] >= 28)"; //Missed Opening bracket '('

就像我们在评估查询表达式。可能是表达可以发辫。?有帮助但如何需要在这方面提供帮助。

Like we have in Evaluate Query Expression. Might be Expression tress can be helpful. But how? Need help in this regard.

推荐答案

看看的 NCalc ,它是用于评估数学表达式的框架。

Take a look at NCalc. It's a framework for evaluating mathematical expressions.

在表达式有语法错误,评估将抛出一个EvaluationException。

When the expression has a syntax error, the evaluation will throw an EvaluationException.

try { new Expression("(3 + 2").Evaluate(); } catch(EvaluationException e) { Console.WriteLine("Error catched: " + e.Message); }

不过,您也可以在评估前使用 HasErrors检测语法错误( 。)法

Though, you can also detect syntax errors before the evaluation by using the HasErrors() method.

Expression e = new Expression("a + b * ("); if(e.HasErrors()) { Console.WriteLine(e.Error); }

更多推荐

评估条件表达式

本文发布于:2023-11-12 10:32:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581236.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   条件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!