{"id":1086,"date":"2011-03-20T16:00:41","date_gmt":"2011-03-20T15:00:41","guid":{"rendered":"http:\/\/www.minddriven.de\/?p=1086"},"modified":"2011-03-26T15:47:59","modified_gmt":"2011-03-26T14:47:59","slug":"wp7-compiling-expressions","status":"publish","type":"post","link":"https:\/\/www.minddriven.de\/index.php\/technology\/dot-net\/windows-phone\/wp7-compiling-expressions","title":{"rendered":"WP7 #3: Compiling Expressions"},"content":{"rendered":"<p>Just a short post today about how to compile Expressions under Windows Phone 7.<\/p>\n<p><strong>Expression Trees<\/strong> 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 <a href=\"http:\/\/www.minddriven.de\/index.php\/technology\/dot-net\/c-sharp\/specification-pattern-obsolete\" target=\"_blank\">Specification Pattern<\/a> and <a href=\"http:\/\/www.minddriven.de\/index.php\/technology\/dot-net\/c-sharp\/efficient-expression-values\" target=\"_blank\">Guard Classes<\/a>. After you&#8217;ve built up your expression tree (which is a tree representation of real code), you can execute your code fragment by calling <code>expression.Compile().Invoke()<\/code>.<\/p>\n<p>When it comes to Windows Phone 7, that&#8217;s a problem&#8230; because there <em>is<\/em> no <code>Compile()<\/code> method on the <code>Expression<\/code> class. Since the WP7 platform has <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ff426930%28v=VS.96%29.aspx#LINQ\" target=\"_blank\">limitations<\/a> at some points (regarding dynamic coding, Reflection, etc.), it doesn&#8217;t have a <code>Compile()<\/code> method at the moment (let&#8217;s hope the MS guys are fixing this in the near future&#8230;).<\/p>\n<p>One solution I came across with can be found <a href=\"http:\/\/siaqodb.com\/?p=220\" target=\"_blank\">here<\/a>. Those guys take the Expression compiler from the <strong>Mono project<\/strong> and use it instead. With a call of <code>ExpressionCompiler.Compile(expression)<\/code> you&#8217;re able to compile an expression. It works, sadly it&#8217;s kind of limited: the Expression compiler makes use of Reflection &#8211; since Silverlight doesn&#8217;t support Reflection on private members, all members used in the Expression must be public. This limits the range of this solution, so let&#8217;s hope that Microsoft will adopt the WP7 platform at this point&#8230;<\/p>\n<p>The sources and binary DLL of the Expression compiler can be found here for free (MIT license): [<a href=\"http:\/\/www.minddriven.de\/wp-content\/uploads\/2011\/03\/ExpressionCompilerForWP7.zip\" target=\"_blank\">Download<\/a>]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;ve built &hellip; <a href=\"https:\/\/www.minddriven.de\/index.php\/technology\/dot-net\/windows-phone\/wp7-compiling-expressions\" class=\"more-link\"><span class=\"screen-reader-text\">WP7 #3: Compiling Expressions<\/span> weiterlesen<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[282],"tags":[279,289,283,284,285],"class_list":["post-1086","post","type-post","status-publish","format-standard","hentry","category-windows-phone","tag-compile","tag-expression","tag-windows-phone-7","tag-wp7","tag-wp7dev"],"_links":{"self":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/1086","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/comments?post=1086"}],"version-history":[{"count":9,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/1086\/revisions"}],"predecessor-version":[{"id":1162,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/1086\/revisions\/1162"}],"wp:attachment":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/media?parent=1086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/categories?post=1086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/tags?post=1086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}