Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a .Net attribute to prevent property evaluation within the debugger?

I've seen several classes in the framework with this behavior (a refresh symbol in the watch window, and a warning). Is this controlled by an Attribute? If so, how can I emulate this in my library?

EDIT: Thanks for the info! To clarify, I am developing a framework with properties that must access data from a single thread. Unfortunately, I am getting some odd behavior when I am in the debugger because of watch windows, etc. I have experience with the Debugger Browsable attribute; however, I would prefer to display the properties after the main thread has accessed / set them. I have seen, especially in IEnumerables that the debugger will not evaluate without user input. ...Is there any way to flag these properties as requiring "Implicit Evaluation", Or can I not have my cake and eat it too?

like image 414
Nescio Avatar asked Feb 21 '09 03:02

Nescio


People also ask

Which of the following attributes can be used to assist with debugging C# code?

The DebuggerDisplayAttribute attribute controls how a type or member is displayed in the debugger variable windows.

What attribute can we use to hide the display of members in the debugger?

DebuggerStepThrough Attribute Sometimes it is more useful to hide a member or type from the debugger completely.

How do you stop a Debug in Visual Basic?

To end a debugging session in Microsoft Visual Studio, from the Debug menu, choose Stop Debugging.

What is .NET debugger?

Debugging allows the developers to see how the code works in a step-by-step manner, how the values of the variables change, how the objects are created and destroyed, etc.


1 Answers

This is not controlled by attributes. It's an inherent feature of the debugger.

The underlying reason for this feature is to prevent unwanted function evaluations by the user. Func Evals are a dangerous operation in the debugger and can cause significant slow downs or corrupt program state. The debugger takes great care to make sure that it doesn't inadvertently do extra func evals that may degrade your debugging experience.

In the case where there is an item in the watch/locals/auto window that may cause a func eval and the debugger does not believe a func eval should happen, the value will grey out and a refresh button will appear in the value column. Clicking on that button is telling the debugger, "no really I want to evaluate that expression".

There are many reasons why this will happen in the debugger. The following 2 though are the most likely.

Implicit Property Evaluation Is Disabled

Tools -> Debugger -> Options -> Enable Implicit Property Evaluation

If this value is unchecked, you are telling the debugger please don't auto-evaluate properties. Properties under the hood are just function calls. They are generally safer than normal function calls but not always.

But you can still force properties to evaluate by typing them directly into the watch window. If you type 2 in a row, the first value will become "stale". This is because typing a second expression in the watch window will cause all other expressions to get re-evaluated. Why? Because the act of evaluating any expression could have altered the results of the others.

Because implicit func eval is turned off the first property will not auto-evaluate and you must force it.

Func Eval and Step

If you add an expression to the watch window which does a function evaluation and then do a step operation, the value will be "staled" in the watch window.

This is done for many reasons, one of the most impactful reasons though is stepping performance. It's very common for a user to type many expressions in the watch window, and it's definitely not rare to have a function evaluation. One at a time these aren't very slow. But imagine you're trying to step quickly through some code and you had 10 func evals in the watch window. That can quickly add up and significantly degrade your stepping experience. So func evals are not automatically re-evaluated.

like image 123
JaredPar Avatar answered Nov 14 '22 23:11

JaredPar