{"id":2833,"date":"2009-11-19T12:05:00","date_gmt":"2009-11-19T12:05:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudio\/2009\/11\/19\/debugging-expression-trees-in-visual-studio-2010\/"},"modified":"2019-02-14T15:42:07","modified_gmt":"2019-02-14T23:42:07","slug":"debugging-expression-trees-in-visual-studio-2010","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/visualstudio\/debugging-expression-trees-in-visual-studio-2010\/","title":{"rendered":"Debugging Expression Trees in Visual Studio 2010"},"content":{"rendered":"<p>First of all, let\u2019s take a look at the example from one of <a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkID=169513\">my previous posts<\/a>. It creates an expression tree for calculating the factorial of a number.<\/p>\n<pre class=\"code\"><span>ParameterExpression <\/span>value =\n    <span>Expression<\/span>.Parameter(<span>typeof<\/span>(<span>int<\/span>), <span>\"value\"<\/span>);\n<span>ParameterExpression <\/span>result =\n    <span>Expression<\/span>.Parameter(<span>typeof<\/span>(<span>int<\/span>), <span>\"result\"<\/span>);\n<span>LabelTarget <\/span>label = <span>Expression<\/span>.Label(<span>typeof<\/span>(<span>int<\/span>));\n<span>BlockExpression <\/span>block = <span>Expression<\/span>.Block(\n    <span>new<\/span>[] { result },\n    <span>Expression<\/span>.Assign(result, <span>Expression<\/span>.Constant(<span>1<\/span>)),\n    <span>Expression<\/span>.Loop(\n        <span>Expression<\/span>.IfThenElse(\n            <span>Expression<\/span>.GreaterThan(value,\n                <span>Expression<\/span>.Constant(<span>1<\/span>)),\n            <span>Expression<\/span>.MultiplyAssign(result,\n                <span>Expression<\/span>.PostDecrementAssign(value)),\n        <span>Expression<\/span>.Break(label, result)\n        ),\n        label\n    )\n);\n<span>Expression<\/span>&lt;<span>Func<\/span>&lt;<span>int<\/span>, <span>int<\/span>&gt;&gt; lambda =\n    <span>Expression<\/span>.Lambda&lt;<span>Func<\/span>&lt;<span>int<\/span>, <span>int<\/span>&gt;&gt;(block, value);\n<span>Console<\/span>.WriteLine(lambda.Compile()(<span>5<\/span>));<\/pre>\n<p><a href=\"http:\/\/11011.net\/software\/vspaste\"><\/a>\nNow, what if you want to see the content of this tree in debug mode? You can try <span style=\"font-family: 'Courier New'\">lambda.ToString()<\/span> but it\u2019s not very informative. All you get is this: <span style=\"font-family: 'Courier New'\">value =&gt; {var result; \u2026 }<\/span>. Basically, it tells you only that the lambda has the <span style=\"font-family: 'Courier New'\">result<\/span> parameter.\nAnother option is to explore the tree structure in the <b>Watch<\/b> window. However, since this is a tree, you need to click through numerous nodes. This gives you a good understanding of the tree structure, but it might be hard to understand the actual content of the tree.\nBut there is a better solution: expression trees now have their own visualizer that helps you to explore their structure and content.\nCopy the above code example into Visual Studio 2010, set a breakpoint at the last line, and press F5. Rest the mouse pointer over the lambda variable and you will see that it has the <strong>DebugView<\/strong> property. This property is private and is exposed only in the debugger, so you can\u2019t use it in your code. But it\u2019s used to hook up the expression trees visualizer. Click the magnifying glass icon and you will see the list of available visualizers.\n<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/4\/2019\/06\/image_2.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2009\/11\/image_thumb-1.png\" width=\"522\" height=\"164\" \/><\/a>\n<a href=\"http:\/\/11011.net\/software\/vspaste\"><\/a><a href=\"http:\/\/11011.net\/software\/vspaste\"><\/a>\nNow click <b>Text Visualizer<\/b> and you get this.\n<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/4\/2019\/06\/image_4.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2009\/11\/image_thumb_1.png\" width=\"524\" height=\"292\" \/><\/a>\nAs you can see, the content of the expression tree is represented in some kind of metalanguage. (No, we couldn&#8217;t use C# here, because expression trees serve other languages as well.)\nThere are some simple rules used in this metalanguage:<\/p>\n<ul>\n<li>Parameters are displayed with a $ symbol at the beginning (for example, $result and $value.)<\/li>\n<li>Integer, string, and null constants are displayed \u201cas is\u201d (for example, $result = 1). For other numeric types that have standard suffixes, the suffix is added to the value. (For example, for decimal the value is 1M, for double 1D, etc.)<\/li>\n<li>If a parameter, label, or lambda expression does not have a name, the name is generated automatically (for example, #Label1, $var1, etc.)<\/li>\n<li>Checked operators are displayed with the # symbol preceding the operator. For example, the checked addition operator is displayed as #+.<\/li>\n<\/ul>\n<p>There are some other nuances that I think are evident and don&#8217;t need additional explanation. But if you don\u2019t understand something in this syntax, let me know and I&#8217;ll include more information in the documentation.\n<strong>Update:<\/strong>\nDocumentation for this feature is now availabe on MSDN: <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ee725345(VS.100).aspx\">Debugging Expression Trees<\/a>.\n<img decoding=\"async\" src=\"\" width=\"1\" height=\"1\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>First of all, let\u2019s take a look at the example from one of my previous posts. It creates an expression tree for calculating the factorial of a number. ParameterExpression value = Expression.Parameter(typeof(int), &#8220;value&#8221;); ParameterExpression result = Expression.Parameter(typeof(int), &#8220;result&#8221;); LabelTarget label = Expression.Label(typeof(int)); BlockExpression block = Expression.Block( new[] { result }, Expression.Assign(result, Expression.Constant(1)), Expression.Loop( Expression.IfThenElse( Expression.GreaterThan(value, [&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,9,185,12,13],"class_list":["post-2833","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-visual-studio","tag-net-framework","tag-c","tag-debug","tag-node-js","tag-visual-studio","tag-visual-studio-2010"],"acf":[],"blog_post_summary":"<p>First of all, let\u2019s take a look at the example from one of my previous posts. It creates an expression tree for calculating the factorial of a number. ParameterExpression value = Expression.Parameter(typeof(int), &#8220;value&#8221;); ParameterExpression result = Expression.Parameter(typeof(int), &#8220;result&#8221;); LabelTarget label = Expression.Label(typeof(int)); BlockExpression block = Expression.Block( new[] { result }, Expression.Assign(result, Expression.Constant(1)), Expression.Loop( Expression.IfThenElse( Expression.GreaterThan(value, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/2833","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=2833"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/2833\/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=2833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/categories?post=2833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/tags?post=2833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}