-
Notifications
You must be signed in to change notification settings - Fork 0
Library usage C#
Maciej Klemarczyk edited this page Oct 22, 2021
·
3 revisions
- Install library from the NuGet.org
- Create instance of
PredicateExpressionwith your expression to evaluate - Call
Preparemethod if you want to reuse the evaluation with different variables - Call
Evaluatemethod with or without passing dictionary with variables - Capture result of
Evaluatemethod which indicates if the predicate is True or False
var exampleExpression = "{<, 42, 182}";
var engine = new PredicateExpression(exampleExpression);
// Optional step to parse the expression before evaluation
engine.Prepare();
bool result = engine.Evaluate();var exampleExpression = "{AND {<, $userId, 182}, {=, logged, $status}}";
exampleExpression = exampleExpression.Replace("$userId", "42");
exampleExpression = exampleExpression.Replace("$status", "logged");
var engine = new PredicateExpression(exampleExpression);
// Optional step to parse the expression before evaluation
engine.Prepare();
bool result = engine.Evaluate();// Dictionary with all required variables
var variables = new Dictionary<string, object>();
variables["userId"] = 42;
variables["status"] = "logged";
variables["permission"] = 13;
var exampleExpression = "{AND {<, $userId, 182}, {=, logged, $status}, {HasFlag, $permission, 4}}";
var engine = new PredicateExpression(exampleExpression);
// Optional step to parse the expression before evaluation
engine.Prepare();
bool result = engine.Evaluate(variables);