I'm creating an expression tree manually like this
var innerAddition = Expression.Add(Expression.Constant(5), Expression.Constant(9));
var mult = Expression.Multiply(innerAddition, Expression.Constant(2));
var top = Expression.Add(Expression.Constant(3), mult);
When I look at DebugView in debug mode, I see 3 + (5 + 9) * 2, which is what I would like to output from my program. I understand this is using the expression tree visualizer. Is there a way to use this from my code? Thanks!
DebugView is a great way to see what your code is doing while an application is running, for example to see what the code for an ASP.NET web application does while browsing the site. DebugView for Windows is available for download from Microsoft’s Sysinternals team.
Debugging Expression Trees in Visual Studio (C#) You can analyze the structure and content of expression trees when you debug your applications. To get a quick overview of the expression tree structure, you can use the DebugView property, which represents expression trees using a special syntax.
The dialog contains two edit fields: include and exclude. The include field is where you enter substring expressions that match debug output lines that you want DebugView to display, and the exclude field is where you enter text for debug output lines that you do not want DebugView to display.
(Note that DebugView is available only in debug mode.) Since DebugView is a string, you can use the built-in Text Visualizer to view it across multiple lines, by selecting Text Visualizer from the magnifying glass icon next to the DebugView label. Alternatively, you can install and use a custom visualizer for expression trees, such as:
Here is a simple extension method to get the internal property's value using reflection.
public static string GetDebugView(this Expression exp)
{
if (exp == null)
return null;
var propertyInfo = typeof(Expression).GetProperty("DebugView", BindingFlags.Instance | BindingFlags.NonPublic);
return propertyInfo.GetValue(exp) as string;
}
The classes that implement the debug view features are intentionally internal
so that you cannot access them without reflection. Although this seems unfair, the purpose of debugging is debugging and it is not intended as a supported API and therefore could change at any time, perhaps to improve debugging! Using the supported public APIs will ensure compatibility with future versions.
Here is another StackOverflow question using the public APIs:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With