(Evaluate Logical (&&,||) expressions) Logic Evaluator in C#

boolean-logic c# dynamic-linq linq logical-operators

Question

My project includes aLogic evaluation section, it accepts a string representing input that compriseslogical expressions (true/false ) .

This string has to be assessed, and I want a final Boolean result.

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

The next time ILogic The length could lengthen.

Is it possible to assess this expression fromLINQ / Dynamic LINQ ?

1
4
12/13/2011 12:03:35 PM

Accepted Answer

To avoid using any external libraries, use a DataTable with expression.

There, you also have the option of evaluating on result value kinds other booleans.

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];

Although the expression syntax in your example is different, it accomplishes the same task. One benefit is that it is completely controlled by Microsoft and has the.NET framework. The management of errors is also acceptable. the absence of operators, etc., exceptions

existing operators

12
12/12/2011 3:47:05 PM

Popular Answer

You may employ Zentrum.

Full disclosure: I was the author.



Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow