C# does not include a built-in method to evaluate a string during runtime, like Javascript's eval() or VBScript's Eval().
ExpressionEvaluator is a lightweight, simple and free library capable of parsing and compiling simple to medium complexity C# expressions. It features a Antlr-based tokenizer and parser and generates a LINQ Expression tree which is then compiled into a function.
Applications for an expression parser and compiler are usually in the form of embedded code or user-defined expressions that need to be executed against runtime objects.
- An Experimental Music Compiler for Windows Mobile
- Dynamic data-bound XML templates
- User-defined conditional code stored in configuration files
- Customer-defined queries
- User-defined execution parameters for pluggable controls
If you have downloaded and used this library, I'd like to know about it's usage! Feel free to let me know about it.
Expression Evaluator is now available via NuGet
See Usage and Sample Expressions under Documentation
For more details see [Updates]
- Compute: Arithmetic operators: +- * / % ^
- Compare: Relational operators: == != < > <= >=
- Assign: Set values with the = operator
- Test: Logical Operators: ! & | (bitwise logic) and && || (short circuit logic)
- Expression grouping with parentheses ( )
- Index accessors {"[ ]"}
- Supports dynamic objects (including classes that implement IDynamicMetaObjectProvider such as ExpandoObject)
- Context: Register and access external types and symbols through the TypeRegistry
- Strings: (enclosed in 'single quotes' and string concatenation with +)
- true, false, null literals
- Declarative typing of numbers using d/f/m/l/u/ul suffixes
- Implicit conversion of numerical expressions
- Member access operator (.) for any valid expression. Access properties, fields and methods of types, objects and expressions
- Pre-registered default types e.g. bool, int, double, float, char, string
- Supports nested function calls (x.method(y.method(z.method()), y.method2()))
Expression Evaluator is 100% free, but if you would like to support the project in any way please do so.
[](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=rupert.avery@gmail.com&lc=PH&item_name=Expression Evaluator¤cy_code=USD&bn=PP-DonationsBF:btn_donateCC_LG.gif:NonHosted)
This is not a fully C#-compliant compiler, and as such features may be missing and there may be bugs or discrepancies in how the parser and compiler work. If you wish to use this library in production code do so at your own risk.