Just a short post today about how to compile Expressions under Windows Phone 7.
Expression Trees are a powerful feature since .NET 3.0. They can be used to create executable code fragments dynamically at runtime and have a wide range of purposes, for example to implement the Specification Pattern and Guard Classes. After you’ve built up your expression tree (which is a tree representation of real code), you can execute your code fragment by calling
When it comes to Windows Phone 7, that’s a problem… because there is no
Compile() method on the
Expression class. Since the WP7 platform has limitations at some points (regarding dynamic coding, Reflection, etc.), it doesn’t have a
Compile() method at the moment (let’s hope the MS guys are fixing this in the near future…).
One solution I came across with can be found here. Those guys take the Expression compiler from the Mono project and use it instead. With a call of
ExpressionCompiler.Compile(expression) you’re able to compile an expression. It works, sadly it’s kind of limited: the Expression compiler makes use of Reflection – since Silverlight doesn’t support Reflection on private members, all members used in the Expression must be public. This limits the range of this solution, so let’s hope that Microsoft will adopt the WP7 platform at this point…
The sources and binary DLL of the Expression compiler can be found here for free (MIT license): [Download]
Ein Gedanke zu „WP7 #3: Compiling Expressions“
Can I use your expression compiler (with permission and included license of course) in my OSS project? It’s here http://servelat.codeplex.com/