DynamicExpresso
DynamicExpresso copied to clipboard
Intermediate resolve variables before final result
Does there exist any intermediate option to resolve the variables by the given parameters brefore the final result? For Example I have
var parsedAmount = interpreter.Parse("x * y + 2", parameters);
And I would like to have the result before the final result returned. E.g.
"4 * 5 + 2"
My current workaround is to do a string replacement, like so:
var expr = "x * y + 2";
var parsedAmount = interpreter.Parse(expr, parameters);;
parsedAmount.UsedParameters.ToList().ForEach(x =>
{
expr = expr .Replace(x.Name, x.Name + $" [{x.Value}]");
});
Interesting idea, something like a "linking" phase ... But no, for now I don't think it is possible. As usual contributions are welcome!
My idea seems pretty limited in scope, but based on this request it would be possible to add in a ParameterInterceptor style functionality. The end user would receive the ParameterExpression, its current value, and if we wanted they could return the value or a different value (this could give them the means to do some extra validation inline).
var expr = "x * y + 2";
var target = new Interpreter();
var updatedText = expr;
target.ParameterInterceptor += (Parameter expression, int? textIndex, object currentValue) =>
{
updatedText = updatedText.Replace(expression.Name, currentValue?.ToString());
return currentValue;
};
var parsedAmount = interpreter.Parse(expr, parameters);
parsedAmount.Invoke(parameters);
It's a pretty limited example that would need a LOT more code on the end user's side if parameter names were used multiple times, but it would give the flexibility needed to see exactly what is being ran.