“Hidden Secrets” of the Visual Studio .NET debugge

2019-01-29 17:42发布

As much as I generally don't like the discussion/subjective posts on SO, I have really come to appreciate the "Hidden Secrets" set of posts that people have put together. They provide a great overview of some commonly missed tools that you might now otherwise discover.

For this question I would like to explore the Visual Studio .NET debugger. What are some of the "hidden secrets" in the VS.NET debugger that you use often or recently discovered and wish you would have known long ago?

14条回答
\"骚年 ilove
2楼-- · 2019-01-29 18:30

One of my favorite features is the "When Hit..." option available on a breakpoint. You can print a message with the value of a variable along with lots of other information, such as:

  • $ADDRESS - Current Instruction
  • $CALLER - Previous Function Name
  • $CALLSTACK - Call Stack
  • $FUNCTION - Current Function Name
  • $PID - Process ID
  • $PNAME - Process Name
  • $TID - Thread ID
  • $TNAME - Thread Name

You can also have it run a macro, but I've never used that feature.

查看更多
叛逆
3楼-- · 2019-01-29 18:30

$exception in the watch window will show the exception that is currently being processed even if you don't have a catch that assign the Exception instance to a named variable.

查看更多
爷、活的狠高调
4楼-- · 2019-01-29 18:31

You can right-click an object in the Watch window and click Make Object ID.

It will assign that instance an ID number, allowing you to see, in a complicated object graph, which objects refer to the same instance.

查看更多
欢心
5楼-- · 2019-01-29 18:31

You can open and place a breakpoint in a source file if the file belongs to another solution (external file). The debugger can still hit the breakpoint. No need to open another Visual Studio instance to debug the external file. Helpful in debugging web services which you source to. This works as long as all the sources are current and compiled.

查看更多
疯言疯语
6楼-- · 2019-01-29 18:32

This is kind of an old one. If you add a watch expression err,hr, then this will hold the result of GetLastError(), formatted as an HRESULT (VC++ debugger only).

查看更多
一纸荒年 Trace。
7楼-- · 2019-01-29 18:38

For .net applications System.Diagnostics has lots of useful debugging things. The Debugger class for example:

Debugger.Break(); // Programmatically set a break point
Debugger.Launch(); // Launch the debugger if not already attached
Debugger.IsAttached // Check if the debugger is attached

System.Diagnostics also has lots of good attributes. The two I've used are the debugger display attribute for changing the details put into the locals window and the step through attribute for skipping code you don't care about debugging:

// Displays the value of Property1 for any "MyClass" instance in the debugger
[DebuggerDisplay("{Property1}")]
public class MyClass {
    public string Property1 { get; set; }

    [DebuggerStepThrough]
    public void DontStepInto() {
       // An action we don't want to debug
    }
}
查看更多
登录 后发表回答