Export (0) Print
Expand All

Debugging Expression Trees (C# and Visual Basic)

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 is available only in debug mode. For more information about debugging, see Debugging in Visual Studio.

To better represent the content of expression trees, the DebugView property uses Visual Studio visualizers. For more information, see Visualizers.

To open a visualizer for an expression tree

  1. Click the magnifying glass icon that appears next to the DebugView property of an expression tree in DataTips, a Watch window, the Autos window, or the Locals window.

    A list of visualizers is displayed.

  2. Click the visualizer you want to use.

Each expression type is displayed in the visualizer as described in the following sections.

ParameterExpression variable names are displayed with a "$" symbol at the beginning.

If a parameter does not have a name, it is assigned an automatically generated name, such as $var1 or $var2.

Examples

Expression

DebugView property

Dim numParam As ParameterExpression = 
Expression.Parameter(GetType(Integer), "num")

$num

Dim numParam As ParameterExpression = 
Expression.Parameter(GetType(Integer))

$var1

For ConstantExpression objects that represent integer values, strings, and null, the value of the constant is displayed.

For numeric types that have standard suffixes as C# literals, the suffix is added to the value. The following table shows the suffixes associated with various numeric types.

Examples

Expression

DebugView property

int num = 10;
ConstantExpression expr = Expression.Constant(num);

Dim num as Integer= 10
Dim expr As ConstantExpression = Expression.Constant(num)

10

double num = 10;
ConstantExpression expr = Expression.Constant(num);

Dim num As Double = 10
Dim expr As ConstantExpression = Expression.Constant(num)

10D

If the type of a BlockExpression object differs from the type of the last expression in the block, the type is displayed in the DebugInfo property in angle brackets (< and >). Otherwise, the type of the BlockExpression object is not displayed.

Examples

Expression

DebugView property

Dim block As BlockExpression = Expression.Block(Expression.Constant("test"))

.Block() {

"test"

}

Dim block As BlockExpression = 
Expression.Block(GetType(Object), Expression.Constant("test"))

.Block<System.Object>() {

"test"

}

LambdaExpression objects are displayed together with their delegate types.

If a lambda expression does not have a name, it is assigned an automatically generated name, such as #Lambda1 or #Lambda2.

Examples

Expression

DebugView property

Dim lambda As LambdaExpression = 
Expression.Lambda(Of Func(Of Integer))(Expression.Constant(1))

.Lambda #Lambda1<System.Func'1[System.Int32]>() {

1

}

Dim lambda As LambdaExpression = 
Expression.Lambda(Of Func(Of Integer))(Expression.Constant(1), "SampleLamda", Nothing)

.Lambda SampleLambda<System.Func'1[System.Int32]>() {

1

}

If you specify a default value for the LabelExpression object, this value is displayed before the LabelTarget object.

The .Label token indicates the start of the label. The .LabelTarget token indicates the destination of the target to jump to.

If a label does not have a name, it is assigned an automatically generated name, such as #Label1 or #Label2.

Examples

Expression

DebugView property

Dim target As LabelTarget = Expression.Label(GetType(Integer), "SampleLabel")
Dim label1 As BlockExpression = Expression.Block(
Expression.Goto(target, Expression.Constant(0)),
Expression.Label(target, Expression.Constant(-1)))

.Block() {

.Goto SampleLabel { 0 };

.Label

-1

.LabelTarget SampleLabel:

}

Dim target As LabelTarget = Expression.Label()
Dim block As BlockExpression = Expression.Block(
Expression.Goto(target), Expression.Label(target))

.Block() {

.Goto #Label1 { };

.Label

.LabelTarget #Label1:

}

Checked operators are displayed with the "#" symbol in front of the operator. For example, the checked addition operator is displayed as #+.

Examples

Expression

DebugView property

Dim expr As Expression = Expression.AddChecked(
Expression.Constant(1), Expression.Constant(2))

1 #+ 2

Dim expr As Expression = Expression.ConvertChecked(
Expression.Constant(10.0), GetType(Integer))

#(System.Int32)10D

Community Additions

ADD
Show:
© 2014 Microsoft