This project is also available on Github

What does this do?

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

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.

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.


See Usage and Sample Expressions under Documentation

Q & A

What can I use it for?

Anywhere you have an expression that needs to be stored as text and should be executed against some runtime objects
  • An Experimental Music Compiler for Windows Mobile
  • Dynamic data-bound XML templates
  • Complex user-defined queries
  • Adding a REPL to your applications

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!

Is Expression Evaluator thread-safe?

Yes. Static fields are only used for instantiating lists and are not modified. Static methods are all helper methods that do not mutate static data, only parameters.

What C# features does Expression Evaluator support?

Take a look at the features list below, and the Features page.

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 consider using the Compile or ScopeCompile methods to pre-compile your expressions into delegates.

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

Will you support C# 5.0 or 6.0 language features?

There are no current plans to support new language features, but if they are easy enough to do and don't require later versions of .NET LINQ to implement, it may be possible to implement them.


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

PM> Install-Package ExpressionEvaluator

Implemented 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)

Buy me a beer!

Programming sure is hard work. A cold beer would really be appreciated!


Last edited Feb 3 at 11:50 AM by RupertAvery, version 85