Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Expression Trees - Dynamic Value Lookup
    primarykey
    data
    text
    <p>I'm building a C# expression tree to evaluate simple expressions. The expression strings are parsed into trees, and respect the basic operators (mathematical, logical and relational) as well as precedence through the use of parantheses.</p> <p>In addition to the types bool, string and integer - I require some of the elements of the expression to be evaluated at runtime. These are represented by templated strings, for example:</p> <pre><code>([firstname] == "bob") &amp;&amp; ([surname] == "builder") </code></pre> <p>The above expression would be evaluated for a (potentially large) number of objects that provide the context for the current expression invocation, for example, in a loop. The templated section would be used reflectively on the current context - e.g. the current user's firstname and surname would be resolved in the example and those values used in the expression resolution rather than the templated strings.</p> <p>One solution would be to resolve the templated value at parse time, that way a constant expression type could be used and the type of the value would be known. However, re-building and re-compiling the expression tree each use would have bad for performance.</p> <p>So, I need an expression type whose: - value type is not known at parse time, and - which is resolved by a method call at invoke time</p> <pre><code>E.g. Desired example of usage in pseudo code ExpressionParser parser = new ExpressionParser(); // parses and builds expression trees MyParsedExpression expression = parser.Parse("([firstname] == 'bob') &amp;&amp; ([surname] == 'builder'"); // wrapper for the parsed expression foreach (Object user in users) { expression.Context = user; Boolean result = expression.EvaluateTruth(); if (result == true) { // do something } } </code></pre> <p>Thanks, fturtle</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload