Hi there,
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.
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:
([firstname] == "bob") && ([surname] == "builder")
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.
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.
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
E.g. Desired example of usage in pseudo code
ExpressionParser parser = new ExpressionParser(); // parses and builds expression trees
MyParsedExpression expression = parser.Parse("([firstname] == 'bob') && ([surname] == 'builder'"); // wrapper for the parsed expression
foreach (Object user in users)
{
expression.Context = user;
Boolean result = expression.EvaluateTruth();
if (result == true)
{
// do something
}
}
Thanks, fturtle