Parameter type-checking for Logical Binary Expressions written as strings in C#?

97 views Asked by At

Imagine a scenario where you want to allow users to supply an application with logical binary expressions as strings, using a set of well-defined parameters. For example:

"isSuperAdmin || userLevel > 5 && isAdmin"

The system is using NReco's Lambda Parser to validate the expression by trying to Parse it:

Expression exp = lambdaParser.Parse("isSuperAdmin || userLevel > 5 && isAdmin");

The above returns an expression if the structure of the expression is valid, throws otherwise. But it cannot apply any type-checking on the parameters E.g. isAdmin must be a boolean value but what if the user has constructed the following:

    "isSuperAdmin || userLevel > 5 && isAdmin > 33"

This expression is syntactically correct and can be parsed, but it will not be evaluated properly because isAdmin is a boolean, not an integer. Is there a way, not necessarily using NReco.LambdaParser, to infer the expected type of each parameter in an expression and validate it?

0

There are 0 answers