在我的项目有一个逻辑评估部分,它需要输入作为包含逻辑表达式字符串( 真/假)。
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#(评估逻辑(安培;&安培;,
发布评论