Expression Evaluator is a lightweight, easy-to-use and free library capable of parsing and compiling C# expressions at runtime.

If you have downloaded and used this library, I'd like to know about how it's being used! Feel free to contact me about it or donate.

Q & A

What does it do?

Expression Evaluator can take a string that contains a C# expression, statement or set of statements, parse it and compile it into a delegate function that you can call from your 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?

See Usage and Sample Expressions under Documentation

I have a non-C# expression that I need to evaluate. Can Expression Evaluator 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 Expression Evaluator support?

Take a look at the Features list.

Does Expression Evaluator support dynamics?

Yes! Most of the time Expression Evaluator will compile calls made against types that implement IDynamicMetaObjectProvider to dynamic calls. Dynamics isn't completely implemented however, so if you do encounter bugs, let us know so we can fix it!

There are a few other C# expression evaluators out there. What does Expression Evaluator offer?
  • 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, local variables, the var keyword
  • Conditionals (if-then-else, switch) and loops (for, do, while, foreach)

My expressions may be called thousands of times in a loop. What is the performance of Expression Evaluator like?

Compilation is a costly process. Most of the time you should be compiling once and executing multiple times. Expression Evaluator has the Eval() method which is fine 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 compiled 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 reference it and call the extension method ToCSharpCode() on your LINQ Expression

NuGet

You can get the latest package through the command line or the Nuget Gallery

PM> Install-Package ExpressionEvaluator

C# Features

  • Arithmetic operators: +- * / % ^
  • Relational operators: == != < > <= >=
  • Logical Operators: ! & | (bitwise logic) and && || (short circuit logic)
  • Expression grouping with parentheses ( )
  • Post increment and decrement operators ++ --
  • Property index accessors [ ]
  • Support for dynamics (e.g. ExpandoObject)
  • 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
  • Set the context for identifiers in an expression (see ScopeCompile)

Donate

Expression Evaluator is 100% free, but if you would like to support the project in any way please do so.

Donate

Last edited Feb 3 at 12:22 PM by RupertAvery, version 77