What is ExpressionEval ?
ExpressionEval is an Expression Evaluator back-end component. It allows you to evaluate arbitrary string expressions sush as: logical (Or, And, Not, Xor), comparison (=, <>, <=, <, >, >=) or calculation (+, -, *, /) in an application. Variables and functions can also be used in expressions. The component is configurable: double decimal separator, string tag,… This component is a library provided in .NET Standard 2.0 and in .NET Framework 4.5.
The component is open-source with full functionnalities.
The code source is here: https://github.com/Pierlam/ExpressionEval
Key features
- Input expression as a simple string,
- The expression can be simple or complex,
- Can have nested expression, one or more,
- Comparison operator possible: Or, And, Xor, Not,
- Comparison operator can be defined in english (Or, And, Xor, Not) or in french (Ou, Et, Oux, Non),
- Logical operator possible: =, <>, >, <, >=, <=,
- Calculation operator managed: +, -, * /,
- Value type managed: string, int, double, bool,
- Predefined boolean value can be used: true/False (in english) vrai/faux (in french),
- You can use variable, one or more,
- You can use function call, with parameters or not,
- Wrong expression error management.
Getting started?
Here is a basic general sample which demonstrate how to use the component.
1/ you provide your expression to the component as a simple string, for example:
(a=b) and (c>12)
2/ The ExpressionEval component parse the expression.
3/ you have to define and set all used variables present in the expression, for example:
a:=12
b:=12
c:=15
4/ Then the component can evaluate the expression and return the result: true.
So the corresponding source code is :
// Using clauses
using Pierlam.ExpressionEval;
// Create the component
ExpressionEval evaluator = new ExpressionEval();
// 1-Create the expression to use
string expr= "(a=b) and (c>12)";
// 2-Parse, Decode the expression
evaluator.Parse(expr);
// 3-Create variable and provide values
evaluator.DefineVarInt("a", 12);
evaluator.DefineVarInt("b", 12);
evaluator.DefineVarInt("c", 15);
// 4-Execute the expression
ExecResult execResult= evaluator.Exec();
// Get the result and display it:
Console.WriteLine("Execution Result: " + execResult.ResultBool);
After execution, the console output displays:
Execution Result: true
Because the evaluation of the expression: (12=12) and (15>12) return true.
To use ExpressionEval in your application, get the component from nuget (the nuget link is below).
Get the component
The last available version is: 0.8.
The library is provided in two targets: .NET Standard 2.0 and .NET 4.5
You can get the packet from nuget here: nuget.org/packages/Pierlam.ExpressionEval
Documentation, resources
See the documentation of the component :here.
Code samples
Some code samples are available on github: https://github.com/Pierlam/ExpressionEvalSamples
.
This github repository contains a Visual Studio solution within several examples.