{"id":3023,"date":"2009-09-14T13:37:00","date_gmt":"2009-09-14T13:37:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudio\/2009\/09\/14\/generating-dynamic-methods-with-expression-trees-in-visual-studio-2010\/"},"modified":"2019-02-14T15:42:21","modified_gmt":"2019-02-14T23:42:21","slug":"generating-dynamic-methods-with-expression-trees-in-visual-studio-2010","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/visualstudio\/generating-dynamic-methods-with-expression-trees-in-visual-studio-2010\/","title":{"rendered":"Generating Dynamic Methods with Expression Trees in Visual Studio 2010"},"content":{"rendered":"<p class=\"MsoNormal\"><font size=\"3\" face=\"Calibri\">Expression trees first appeared in Visual Studio 2008, where they were mainly used by LINQ providers. You can use expression trees to represent code in a tree-like format, where each node is an expression. You can also convert expression trees into compiled code and run it. This transformation enables dynamic modification of executable code as well as the execution of LINQ queries in various databases and the creation of dynamic queries. Expression trees in Visual Studio 2008 are explained in Charlie Calvert&rsquo;s blog post <\/font><a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkID=153998\"><font color=\"#0000ff\" size=\"3\" face=\"Times New Roman\">Expression Tree Basics<\/font><\/a><font size=\"3\" face=\"Calibri\">.<\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\" face=\"Calibri\">In this post I&rsquo;m going to show how expression trees were extended in Visual Studio 2010 and how you can use them to generate dynamic methods (a problem that previously could be solved only by emitting MSIL). But although I strongly recommend reading Charlie&rsquo;s blog post first, I still need to repeat some basics to spell out certain nuances.<\/font><\/p>\n<h2><font size=\"4\" face=\"Cambria\">Creating Expression Trees<\/font><\/h2>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">The easiest way to generate an expression tree is to create an instance of the <\/font><span>Expression&lt;T&gt;<\/span><font face=\"Calibri\"> type, where <\/font><span>T<\/span><font face=\"Calibri\"> is a delegate type, and assign a lambda expression to this instance. Let&rsquo;s take a look at the code.<\/font><\/font><\/p>\n<p><span><\/p>\n<p class=\"MsoNormal\"><span>\/\/ Creating an expression tree by providing a lambda expression.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>Expression<\/span><span><font color=\"#000000\">&lt;<\/font><span>Action<\/span><font color=\"#000000\">&lt;<\/font><span>int<\/span><font color=\"#000000\">&gt;&gt; printExpr = (arg) =&gt; <\/font><span>Console<\/span><font color=\"#000000\">.WriteLine(arg);<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><\/p>\n<p><font color=\"#000000\">&nbsp;<\/font><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>\/\/ Compiling and invoking the expression tree.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><font color=\"#000000\">printExpr.Compile()(10);<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormalCxSpFirst\"><span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormalCxSpMiddle\"><span>\/\/ Prints 10.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormalCxSpLast\">\n<p><\/span><font size=\"3\"><font face=\"Calibri\">In this example, the C# compiler generates the expression tree from the provided lambda expression. Note that if you use <\/font><span>Action&lt;int&gt;<\/span><font face=\"Calibri\"> instead of <\/font><span>Expression&lt;Action&lt;int&gt;&gt;<\/span><font face=\"Calibri\"> as a type of the <\/font><span>printExpr<\/span><font face=\"Calibri\"> object, no expression tree will be created, because delegates are not converted into expression trees.<\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">However, this is not the only way to create an expression tree. You can also use classes and methods from the <\/font><span>System.LINQ.Expressions<\/span><font face=\"Calibri\"> namespace. For example, you can create the same expression tree by using the following code.<\/font><\/font><\/p>\n<p class=\"MsoNormalCxSpLast\"><span>\/\/ Creating a parameter for the expression tree.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>ParameterExpression<\/span><span> param = <span>Expression<\/span>.Parameter(<span>typeof<\/span>(<span>int<\/span>), <span>&#8220;arg&#8221;<\/span>);<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>\/\/ Creating an expression for the method call and specifying its parameter.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>MethodCallExpression<\/span><span> methodCall = <span>Expression<\/span>.Call(<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>typeof<\/span><span>(<span>Console<\/span>).GetMethod(<span>&#8220;WriteLine&#8221;<\/span>, <span>new<\/span> <span>Type<\/span>[] { <span>typeof<\/span>(<span>int<\/span>) }),<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>param<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>);<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>\/\/ Compiling and invoking the methodCall expression.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>Expression<\/span><span>.Lambda&lt;<span>Action<\/span>&lt;<span>int<\/span>&gt;&gt;(<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>methodCall,<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>new<\/span><span> <span>ParameterExpression<\/span>[] { param }<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>).Compile()(10);<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormalCxSpFirst\"><span>\/\/ Also prints 10.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font size=\"3\" face=\"Calibri\">Of course this looks much more complicated, but this is what actually happens when you supply a lambda expression to an expression tree.<\/font><\/p>\n<h2><font size=\"4\" face=\"Cambria\">Expression Trees vs. Lambda Expressions<\/font><\/h2>\n<p class=\"MsoNormal\"><font size=\"3\" face=\"Calibri\">A common misconception is that expression trees are identical to lambda expressions. This is not true. On the one hand, as I have already shown, you can create and modify expression trees by using API methods, without using lambda expression syntax at all. On the other hand, not every lambda expression can be implicitly converted into an expression tree. For example, multiline lambdas (also called <i>statement lambdas<\/i>) cannot be implicitly converted into expression trees. <\/font><\/p>\n<p class=\"MsoNormalCxSpLast\"><span>\/\/ You can use multiline lambdas in delegates.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>Action<\/span><span>&lt;<span>int<\/span>&gt; printTwoLines = (arg) =&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>{<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>Console<\/span><span>.WriteLine(<span>&#8220;Print arg:&#8221;<\/span>);<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>Console<\/span><span>.WriteLine(arg);<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>};<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>\/\/ But in expression trees this generates a compiler error.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>Expression<\/span><span>&lt;<span>Action<\/span>&lt;<span>int<\/span>&gt;&gt; printTwoLinesExpr = (arg) =&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>{<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>Console<\/span><span>.WriteLine(<span>&#8220;Print arg:&#8221;<\/span>);<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>Console<\/span><span>.WriteLine(arg);<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>};<\/p>\n<p><\/span><\/p>\n<h2><font size=\"4\" face=\"Cambria\">Expression Trees in Visual Studio 2010<\/font><\/h2>\n<p class=\"MsoNormal\"><font size=\"3\" face=\"Calibri\">All the code examples I have shown so far work (or don&rsquo;t work) the same in both Visual Studio 2008 and Visual Studio 2010. Now let&rsquo;s move to C# 4.0 and Visual Studio 2010.<\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\" face=\"Calibri\">In Visual Studio 2010, the expression trees API was extended and added to the dynamic language runtime (DLR), so that language implementers can emit expression trees rather than MSIL. To support this new goal, control flow and assignment features were added to expression trees: loops, conditional blocks, try-catch blocks, and so on. <\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\" face=\"Calibri\">There is a catch: You cannot use these new features &ldquo;an easy way&rdquo;, by using lambda expressions syntax. You must use the expression trees API. So the last code example in the previous section still generates a compiler error, even in Visual Studio 2010. <\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">But now you have a way to create such an expression tree by using API methods that were not available in Visual Studio 2008. One of these methods is <\/font><span>Expression.Block<\/span><font face=\"Calibri\">, which enables the execution of several expressions sequentially, and this is exactly the method that I need for this example.<\/font><\/font><\/p>\n<p class=\"MsoNormalCxSpLast\"><span>\/\/ Creating a parameter for an expression tree.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>ParameterExpression<\/span><span> param = <span>Expression<\/span>.Parameter(<span>typeof<\/span>(<span>int<\/span>), <span>&#8220;arg&#8221;<\/span>);<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>\/\/ Creating an expression for printing a constant string.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>MethodCallExpression<\/span><span> firstMethodCall = <span>Expression<\/span>.Call(<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>typeof<\/span><span>(<span>Console<\/span>).GetMethod(<span>&#8220;WriteLine&#8221;<\/span>, <span>new<\/span> <span>Type<\/span>[] { <span>typeof<\/span>(<span>String<\/span>) }),<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>Expression<\/span><span>.Constant(<span>&#8220;Print arg:&#8221;<\/span>)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>);<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>\/\/ Creating an expression for printing a passed argument.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>MethodCallExpression<\/span><span> secondMethodCall = <span>Expression<\/span>.Call(<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>typeof<\/span><span>(<span>Console<\/span>).GetMethod(<span>&#8220;WriteLine&#8221;<\/span>, <span>new<\/span> <span>Type<\/span>[] { <span>typeof<\/span>(<span>int<\/span>) }),<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>param<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>);<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>\/\/ Creating a block expression that combines two method calls.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>BlockExpression<\/span><span> block = <span>Expression<\/span>.Block(firstMethodCall, secondMethodCall);<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>\/\/ Compiling and invoking an expression tree.<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>Expression<\/span><span>.Lamb\nda&lt;<span>Action<\/span>&lt;<span>int<\/span>&gt;&gt;(<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>block,<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>new<\/span><span> <span>ParameterExpression<\/span>[] { param }<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>).Compile()(10); <\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font size=\"3\" face=\"Calibri\">I&rsquo;ll repeat this: Although the expression trees API was extended, the way expression trees work with lambda expression syntax did not change. This means that LINQ queries in Visual Studio 2010 have the same features (and the same limitations) that they had in Visual Studio 2008.<\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\" face=\"Calibri\">But because of the new features, you can find more areas outside of LINQ where you can use expression trees. <\/font><\/p>\n<h2><font size=\"4\" face=\"Cambria\">Generating Dynamic Methods<\/font><\/h2>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">Now let&rsquo;s move to the real problems where the new API can help. The most well-known one is creating dynamic methods. The common solution to this problem is to use <\/font><span>System.Reflection.Emit<\/span><font face=\"Calibri\"> and work directly with MSIL. Needless to say, the resulting code is hard to write and read. <\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\" face=\"Calibri\">Basically, the expression tree that prints two lines to the console that I have shown previously is already an example of a dynamic method. But let&rsquo;s try a little bit more complex one to demonstrate more features of the new API.<span>&nbsp; <\/span>Thanks to John Messerly, a developer on the DLR team, for providing the following example.<\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\" face=\"Calibri\">Assume that you have a simple method that calculates the factorial of a number.<\/font><\/p>\n<p class=\"MsoNoSpacing\"><span>static<\/span><span> <span>int<\/span> CSharpFact(<span>int<\/span> value)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>{<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><span>&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp; <\/span><span>int<\/span> result = 1;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>while<\/span><span> (value &gt; 1)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>{<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>result *= value&#8211;;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>}<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>return<\/span><span> result;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>}<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font size=\"3\" face=\"Calibri\">Now you want a dynamic method that does the same thing. We have several essential elements here: a parameter that is passed to a method, a local variable, and a loop. This is how you can represent these elements by using the expression trees API. <\/font><\/p>\n<p><span><span><\/p>\n<p class=\"MsoNoSpacing\"><span>static<\/span><span><font color=\"#000000\"> <\/font><span>Func<\/span><font color=\"#000000\">&lt;<\/font><span>int<\/span><font color=\"#000000\">, <\/font><span>int<\/span><font color=\"#000000\">&gt; ETFact()<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><font color=\"#000000\">{<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><\/p>\n<p><font color=\"#000000\">&nbsp;<\/font><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>\/\/ Creating a parameter expression.<\/span><span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>ParameterExpression<\/span><span><font color=\"#000000\"> value = <\/font><span>Expression<\/span><font color=\"#000000\">.Parameter(<\/font><span>typeof<\/span><font color=\"#000000\">(<\/font><span>int<\/span><font color=\"#000000\">), <\/font><span>&#8220;value&#8221;<\/span><font color=\"#000000\">);<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><\/p>\n<p><font color=\"#000000\">&nbsp;<\/font><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>\/\/ Creating an expression to hold a local variable. <\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>ParameterExpression<\/span><span><font color=\"#000000\"> result = <\/font><span>Expression<\/span><font color=\"#000000\">.Parameter(<\/font><span>typeof<\/span><font color=\"#000000\">(<\/font><span>int<\/span><font color=\"#000000\">), <\/font><span>&#8220;result&#8221;<\/span><font color=\"#000000\">);<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;<\/span><\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><font color=\"#000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>\/\/ Creating a label to jump to from a loop.<\/span><span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>LabelTarget<\/span><span><font color=\"#000000\"> label = <\/font><span>Expression<\/span><font color=\"#000000\">.Label(<\/font><span>typeof<\/span><font color=\"#000000\">(<\/font><span>int<\/span><font color=\"#000000\">));<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><\/p>\n<p><font color=\"#000000\">&nbsp;<\/font><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>\/\/ Creating a method body.<\/span><span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>BlockExpression<\/span><span><font color=\"#000000\"> block = <\/font><span>Expression<\/span><font color=\"#000000\">.Block(<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><\/p>\n<p><font color=\"#000000\">&nbsp;<\/font><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>\/\/ Adding a local variable.<\/span><span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>new<\/span><span><font color=\"#000000\">[] { result },<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><\/p>\n<p><font color=\"#000000\">&nbsp;<\/font><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>\/\/ Assigning a constant to a local variable: result = 1<\/span><span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>Expression<\/span><span><font color=\"#000000\">.Assign(result, <\/font><span>Expression<\/span><font color=\"#000000\">.Constant(1)),<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><\/p>\n<p><font color=\"#000000\">&nbsp;<\/font><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>\/\/ Adding a loop.<\/span><span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span>Expression<\/span><span><font color=\"#000000\">.Loop(<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNoSpacing\"><span><\/span><\/p>\n<p><\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Expression trees first appeared in Visual Studio 2008, where they were mainly used by LINQ providers. You can use expression trees to represent code in a tree-like format, where each node is an expression. You can also convert expression trees into compiled code and run it. This transformation enables dynamic modification of executable code as [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":255385,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[155],"tags":[3,1383,185,13],"class_list":["post-3023","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-visual-studio","tag-net-framework","tag-c","tag-node-js","tag-visual-studio-2010"],"acf":[],"blog_post_summary":"<p>Expression trees first appeared in Visual Studio 2008, where they were mainly used by LINQ providers. You can use expression trees to represent code in a tree-like format, where each node is an expression. You can also convert expression trees into compiled code and run it. This transformation enables dynamic modification of executable code as [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/3023","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/comments?post=3023"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/3023\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media\/255385"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media?parent=3023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/categories?post=3023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/tags?post=3023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}