Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Expression.DebugView from code

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!

like image 628
Shane Fulmer Avatar asked May 14 '11 03:05

Shane Fulmer


People also ask

What is debugview and how do I use it?

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.

How do I debug an expression tree in Visual Studio Code?

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.

How do I include and exclude text in debugview dialog?

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.

How do I view debugview across multiple lines of text?

(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:


2 Answers

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;
}
like image 51
Ben Gripka Avatar answered Sep 26 '22 17:09

Ben Gripka


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:

  • Print out Linq Expression Tree Hierarchy
like image 33
Rick Sladkey Avatar answered Sep 25 '22 17:09

Rick Sladkey