Expression.Compile() on WindowsPhone 7

An interesting thing on WindowsPhone 7 platform is that sometimes a LambdaExpression can be compiled and sometimes not; how come? we was confused too and reason is that Reflection.Emit is not supported on WindowsPhone7 and Expression.Compile() sometimes use Reflection.Emit and sometimes not, depends of your expression so sometimes works and sometimes not.
[Edited]Compile() method of an expression was available in Preview release of WP7 SDK, in the RTM, MS removed Compile () method completely [/Edited]
Problem was confirmed by Microsoft : http://social.msdn.microsoft.com/Forums/en/windowsphone7series/thread/4469af62-66ca-44b0-a256-1bdd228b2539 and let’s hope will be found a solution by them in future but till then what we can do? because Siaqodb use this method for LINQ queries and ofcourse “sometimes” not work (some LINQ queries).

So we download latest stable sources of Mono 2.6.4 and try a hack maybe it is a way to compile a LambdaExpression from MS System.Core with Mono Expression compiler.

And yeah we managed to Compile a MS System.Core LambdaExpression with Mono Expression compiler implementation, NOT using Reflection.Emit with just small changes of Mono sources. We made an assembly: ExpressionCompiler.dll (has only 36KB) and developer can do: ExpressionCompiler.Compile(LambdaExpression expression); instead of default: expression.Compile(). Of course all sources we published and can be downloaded for free (according with MIT license), also ExpressionCompiler assembly can be used in any project(not only with Siaqodb).

Sources are included in new Siaqodb trial setup.
One thing to notice : Mono expression compiler use Reflection to access for example a private field you use in a expression , so since Silverlight not support Reflection on private members(only from class itself you can access by reflection) means you need to make your field/fields, used in expression, public.

Shout it kick it on DotNetKicks.com

Comments

  1. Nice, thank you!

    Posted by Matthias

    Link | March 20th, 2011 at 10:33 am

  2. [...] 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 [...]

    Posted by WP7 #4: Compiling Expressions | Mind-driven development

    Link | March 20th, 2011 at 11:02 am

Leave Your Comment