Share via


Evaluate a watch window expression

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Important

In Visual Studio 2015, this way of implementing expression evaluators is deprecated. For information about implementing CLR expression evaluators, see CLR expression evaluators and Managed expression evaluator sample.

When execution pauses, Visual Studio calls the debug engine (DE) to determine the current value of each expression in its watch list. The DE evaluates each expression using an expression evaluator (EE), and Visual Studio displays its value in the Watch window.

Following is an overview of how a watch list expression is evaluated:

  1. Visual Studio calls the DE's GetExpressionContext to get an expression context that can be used to evaluate expressions.

  2. For each expression in the watch list, Visual Studio calls ParseText to convert the expression text into a parsed expression.

  3. IDebugExpressionContext2::ParseText calls Parse to do the actual work of parsing the text and produce an IDebugParsedExpression object.

  4. IDebugExpressionContext2::ParseText creates an IDebugExpression2 object and puts the IDebugParsedExpression object into it. This IDebugExpression2 object is then returned to Visual Studio.

  5. Visual Studio calls EvaluateSync to evaluate the parsed expression.

  6. IDebugExpression2::EvaluateSync passes the call to EvaluateSync to do the actual evaluation and produce an IDebugProperty2 object that is returned to Visual Studio.

  7. Visual Studio calls GetPropertyInfo to obtain the value of the expression that is then displayed in the watch list.

Parse then evaluate

Since parsing a complex expression can take much longer than evaluating it, the process of evaluating an expression is broken up into two steps: 1) parse the expression and 2) evaluate the parsed expression. This way, evaluation can occur many times but the expression needs to be parsed only once. The intermediate parsed expression is returned from the EE in an IDebugParsedExpression object that is in turn encapsulated and returned from the DE as an IDebugExpression2 object. The IDebugExpression object defers all evaluation to the IDebugParsedExpression object.

Note

It is not necessary for an EE to adhere to this two-step process even though Visual Studio assumes this; the EE can parse and evaluate in the same step when EvaluateSync is called (this is how the MyCEE sample works, for example). If your language can form complex expressions, you may want to separate the parse step from the evaluation step. This can increase performance in the Visual Studio debugger when many watch expressions are being shown.

In this section

Sample implementation of expression evaluation Uses the MyCEE sample to step through the process of expression evaluation.

Evaluating a watch expression Explains what happens after a successful expression parse.

Evaluation context Provides the arguments that are passed when the debug engine (DE) calls the expression evaluator (EE).

See also

Writing a CLR expression evaluator