逻辑计算器在C#(评估逻辑(安培;&安培;,

编程入门 行业动态 更新时间:2024-10-14 06:18:33
本文介绍了逻辑计算器在C#(评估逻辑(安培;&安培;,||)表达式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的项目有一个逻辑评估部分,它需要输入作为包含逻辑表达式字符串( 真/假)。

In my project there is a Logic evaluation section, it take input as a string which contains logical expressions (true/false) .

我要评估这个字符串并返回一个最终的布尔值。

I want to evaluate this string and return a final Boolean value.

string Logic="1&0|1&(0&1)" //string Logic="true AND false OR true AND (false AND true)"

这将是我的逻辑。长度可能会增加。

This will be my Logic. The length might increase.

有什么办法来评估来自 LINQ /动态LINQ 这个表达式?

Is there any way to Evaluate this expression from LINQ / Dynamic LINQ ?

推荐答案

没有任何第三方库的方法是使用与表达的DataTable。

a way without any third party libraries is to use a DataTable with expression.

有你甚至在其他的结果值类型不仅仅是布尔评估的可能性。

There you have even the possibility to evaluate on other result value types than just boolean.

System.Data.DataTable table = new System.Data.DataTable(); table.Columns.Add("", typeof(Boolean)); table.Columns[0].Expression = "true and false or true"; System.Data.DataRow r = table.NewRow(); table.Rows.Add(r); Boolean result = (Boolean)r[0];

的表达式语法与您的例子相同,但它做同样的事情。一个优点是它的100%的.NET框架所载 - >微软管理。 中的错误处理是不坏都不是。缺少运营商的异常等等...

the expression syntax is not identical with your example but it does the same thing. An advantage is that its 100% .NET framework contained --> Microsoft managed. The error handling is not bad neither. Exceptions for missing operators etc...

的可用的运算符

更多推荐

逻辑计算器在C#(评估逻辑(安培;&安培;,

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

发布评论

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

>www.elefans.com

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