Visual Studio调试器技巧& .NET技巧 [英] Visual Studio debugger tips & tricks for .NET

查看:136
本文介绍了Visual Studio调试器技巧& .NET技巧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在VS的调试器工作了好几年了,但是现在我遇到了以前从来没有注意到的一个功能,并且认为:可怜的,我怎么会错过了? >有用!



[免责声明:这些提示适用于VS 2005的C#项目,不保证VS或其他语言的旧版本]



跟踪对象实例



使用给定类的多个实例?你怎么能把他们分开?
在预垃圾收集编程日期,很容易跟踪引用 - 只需查看内存地址。使用.NET,您不能这样做 - 对象可以移动。
幸运的是,手表视图允许您右键单击手表,然后选择创建对象ID。



手表视图http://img403.imageshack.us/img403/461/52518188cq3.jpg



在实例的值之后追加{1#},{2#}等等,有效地为实例赋予了独特的标签。看起来像这样:



数字实例http://img383.imageshack.us/img383/7351/11732685bl8.jpg





标签在该对象的生命周期中保留。



观察变量的有意义的值



默认情况下,观察变量的值是它的类型。如果你想看到它的字段,你必须扩展它,这可能需要很长时间(甚至超时!)如果有很多字段或他们做一些复杂的事情。



然而,一些预定义的类型显示更有意义的信息:




  • 字符串显示其实际内容

  • 列表和字典显示其元素数等。



有意义的信息http://img205.imageshack.us/img205/4808/37220487md1.jpg



不会是很高兴为我自己的类型



嗯...



...有一些质量时间与.NET反射器显示如何容易这可以使用我的自定义类型的 DebuggerDisplay 属性完成:

  [系统。 Diagnostics.DebuggerDisplay(Employee:'{Name}')] 
public class Employee {
public string Name {get {...}}
...
}

...重新运行,... ...



ta da! http://img60.imageshack.us/img60/926/79816018ha1.jpg



这里有更多关于这个主题的信息:MSDN



打破所有异常



...即使是在代码中处理的那些!
我知道,自从我出生以来,我就不知道这件事了,但是在这里,无论如何 - 这可能有一天会帮助某人:



每次抛出异常时,都可以强制调试进程进入调试模式。过去几个小时只是为了遇到这样一段代码呢?

  try {
runStrangeContraption ();
} catch(Exception ex){
/ * TODO:稍后将处理此错误* /
}

在这些情况下,捕捉所有异常非常方便。
这可以从调试>例外...(Ctrl-Alt-E)启用。在Thrown列中勾选出您需要的每种类型的异常。






那些是几个额头我的时刻
你会分享你的吗?

解决方案

两个代码诀窍:



我真的很喜欢 System.Diagnostics .DebuggerStepThrough 属性;您可以将其附加到类,方法或属性,以使VS在调试时默认不输入代码。我更喜欢 DebuggerHidden 属性,因为它将如果您真的需要调试,仍然允许您在忽略的代码中设置断点。



另一个(有时)有用的调用是 System.Diagnostics.Debugger.Launch();当执行命中它时,您将看到选择一个调试器对话框,调试器将开始。有点粗鲁,但是特别令人讨厌的附加到进程是有用的,例如由另一个进程产生的进程,并立即执行你的代码。


I've been working for years with VS's debugger, but every now and then I come across a feature I have never noticed before, and think "Damn! How could I have missed that? It's so useful!"

[Disclaimer: These tips work in VS 2005 on a C# project, no guarantees for older incarnations of VS or other languages]

Keep track of object instances

Working with multiple instances of a given class? How can you tell them apart? In pre-garbage collection programming days, it was easy to keep track of references - just look at the memory address. With .NET, you can't do that - objects can get moved around. Fortunately, the watches view lets you right-click on a watch and select 'Make Object ID'.

watches view http://img403.imageshack.us/img403/461/52518188cq3.jpg

This appends a {1#}, {2#} etc. after the instance's value, effectively giving the instance a unique label. It looks like this:

numbered instance http://img383.imageshack.us/img383/7351/11732685bl8.jpg

The label is persisted for the lifetime of that object.

Meaningful values for watched variables

By default, a watched variable's value is it's type. If you want to see its fields, you have to expand it, and this could take a long time (or even timeout!) if there are many fields or they do something complicated.

However, some predefined types show more meaningful information :

  • strings show their actual contents
  • lists and dictionaries show their elements count etc.

meaningful info http://img205.imageshack.us/img205/4808/37220487md1.jpg

Wouldn't it be nice to have that for my own types?

Hmm...

...some quality time with .NET Reflector shows how easily this can be accomplished with the DebuggerDisplay attribute on my custom type:

[System.Diagnostics.DebuggerDisplay("Employee: '{Name}'")]
public class Employee {
    public string Name { get { ... } }
    ...
}

... re-run, and...

ta da! http://img60.imageshack.us/img60/926/79816018ha1.jpg

There's a lot more info on the subject here: MSDN

Break on all exceptions

... even the ones that are handled in code! I know, I'm such a n00b for not knowing about this ever since I was born, but here it goes anyway - maybe this will help someone someday:

You can force a debugged process to break into debug mode each time an exception is thrown. Ever went on a bug hunt for hours only to come across a piece of code like this?

try {
    runStrangeContraption();
} catch(Exception ex) {
    /* TODO: Will handle this error later */
}

Catching all exceptions is really handy in these cases. This can be enabled from Debug > Exceptions... (Ctrl-Alt-E). Tick the boxes in the 'Thrown' column for each type of exception you need.


Those were a few forehead-slapping moments for me. Would you care to share yours?

解决方案

Two in-code tricks:

I really like the System.Diagnostics.DebuggerStepThrough attribute; you can attach it to a class, method or property to make VS not enter the code by default when debugging. I prefer it over the DebuggerHidden attribute as it will still allow you to put breakpoints in the ignored code if you really need to debug it.

Another (sometimes) useful call is System.Diagnostics.Debugger.Launch(); when the execution hits it, you will be presented with the "select a debugger" dialog, and a debugger will start. A bit rude, but useful with particularly nasty to attach to processes, like a process that gets spawned by another and immediately executes your code.

这篇关于Visual Studio调试器技巧& .NET技巧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆