Expression Evaluator is a fast-growing, lightweight, simple and free library capable of parsing and compiling simple to medium complexity C# expressions.
If you have downloaded and used this library, I'd like to know about it's usage! Feel free to contact me about it.
Starting with version 2, Expression Evaluator gets a boost in stability and capability with the ANTLR-based tokenizer and parser. The use of ANTLR allows development to focus on implementing C# language features.
ANTLR uses a grammar file to generate code in the same way T4 templates work. If you want to edit the g3 grammar file in Visual Studio make sure to download the ANTLR extension
Q & A
What does it do?
Expression Evaluator (EE) can take a string that contains C# code, compile it and return the value of the expression, or a function that executes the compiled code. You can also register types or instances of classes to access their properties and methods, essentially allowing you to dynamically interact with those objects at runtime.What can I use it for?
Anywhere you have something that can be scripted and needs to be executed against your runtime objects
How do I use it?
- An Experimental Music Compiler for Windows Mobile
- Dynamic data-bound XML templates
- User-defined queries
- Adding REPL to your applications
and Sample Expressions
under DocumentationI have a non-C# expression that I need to evaluate. Can EE still be of use to me?
It depends. If it can be pre-processed to look like C# code, then yes!What C# features does EE support?
Take a look at the Features
list.Is dynamics supported by EE?
Yes! Most of the time EE will compile calls made against object type and IDynamicMetaObjectProvider implementers to dynamic calls. If you do encounter bugs, let us know so we can fix it!There are a few libraries out there that are C# expression evaluators. What does EE offer?
My expressions will be called thousands of times. How is the performance of EE?
- Code is compiled, not interpreted, this results in faster execution for subsequent calls
- Support for dynamics
- Setting the "context" for an expression with ScopeCompile
- Multi-statement expressions
- Conditionals (if-then-else, switch) and loops (for, do, while, foreach)
EE has the Eval() method which is good for one-off calls, but if you need to call an expression repeatedly you would be better off using the Compile or ScopeCompile methods. See Performance
on how you can get the most out of your expressions.How can I convert my Expression Tree back into a string?
You can try my fork of mono.linq.expressions
. It has been updated to support dynamics. It is completely standalone and has no dependencies on EE. To use, simply include it and call the extension method ToCSharpCode()
on your LINQ Expression
You can get the latest package through the command line or the Nuget Gallery
PM> Install-Package ExpressionEvaluator
- Arithmetic operators: +- * / % ^
- Relational operators: == != < > <= >=
- Logical Operators: ! & | (bitwise logic) and && || (short circuit logic)
- Expression grouping with parentheses ( )
- Post increment and decrement operators ++ --
- Index accessors [ ]
- Supports calling methods and properties and indexed properties on dynamic objects (such as ExpandoObject)
- Access external variables through a class instance, a global object, or a scope parameter supplied to the compiled function (ScopeCompile)
- 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
- Registry of external types and objects
- Recognizes some default types (bool, int, double, float, char, string, DateTime, Convert, Math)
- Nested function calls (x.method(y.method(z.method()), y.method2()))
- object instantiation with the new keyword
- If-then-else and switch statements
- Compile multiple statements into an expression block
- Local variables and var keyword
- for/while/do loops, break and continue
- foreach loops
Expression Evaluator is 100% free, but if you would like to support the project in any way please do so.