Access Expression.DebugView from code

I create 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 watch DebugView in debug mode, I see 3 + (5 + 9) * 2, which I would like to output from my program. I understand that this is using the expression tree visualizer. Is there any way to use this from my code? Thank!

+3
source share
4 answers

, , internal, . , - , API , , ! API .

StackOverflow API:

+5

, , .

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;
}
+5

http://referencesource.microsoft.com/#System.Core/Microsoft/Scripting/Ast/ExpressionStringBuilder.cs gives the mouse text that you see for expression in Visual Studio when debugging.

Depending on your needs, either this or DebugViewWriter.cs, as Andrew Jackson mentioned, should cover everything.

+1
source

All Articles