C# - Evaluate logical expression from string

/// <summary> /// Evaluates a dynamically built logical expression /// Implement try catch when calling this function : if an exception is thrown, the logical expression is not valid /// </summary> /// <param name="ruleDefinitionWithResults">True AND False OR True</param> /// <returns></returns> public static bool EvaluateLogicalExpression(string logicalExpression) { System.Data.DataTable table = new System.Data.DataTable(); table.Columns.Add("", typeof(bool)); table.Columns[0].Expression = logicalExpression; System.Data.DataRow r = table.NewRow(); table.Rows.Add(r); bool result = (Boolean)r[0]; return result; }
Evaluates a dynamically built logical expression, like "True AND False OR (True AND False)"

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.