Visual Studio debugger tips & tricks for .NET -
i've been working years vs's debugger, every , come across feature have never noticed before, , think "damn! how have missed that? it's so useful!"
[disclaimer: these tips work in vs 2005 on c# project, no guarantees older incarnations of vs or other languages]
keep track of object instances
working multiple instances of given class? how can tell them apart? in pre-garbage collection programming days, easy keep track of references - @ memory address. .net, can't - objects can moved around. fortunately, watches view lets right-click on watch , select 'make object id'.
watches view http://img403.imageshack.us/img403/461/52518188cq3.jpg
this appends {1#}, {2#} etc. after instance's value, giving instance unique label. looks this:
numbered instance http://img383.imageshack.us/img383/7351/11732685bl8.jpg
the label persisted lifetime of object.
meaningful values watched variables
by default, watched variable's value it's type. if want see fields, have expand it, , take long time (or timeout!) if there many fields or complicated.
however, predefined types show more meaningful information :
- strings show actual contents
- lists , dictionaries show elements count etc.
meaningful info http://img205.imageshack.us/img205/4808/37220487md1.jpg
wouldn't nice have own types?
hmm...
...some quality time .net reflector shows how can accomplished debuggerdisplay
attribute on custom type:
[system.diagnostics.debuggerdisplay("employee: '{name}'")] public class employee { public string name { { ... } } ... }
... re-run, and...
ta da! http://img60.imageshack.us/img60/926/79816018ha1.jpg
there's lot more info on subject here: msdn
break on exceptions
... ones handled in code! know, i'm such n00b not knowing ever since born, here goes anyway - maybe someday:
you can force debugged process break debug mode each time exception thrown. ever went on bug hunt hours come across piece of code this?
try { runstrangecontraption(); } catch(exception ex) { /* todo: handle error later */ }
catching exceptions handy in these cases. can enabled debug > exceptions... (ctrl-alt-e). tick boxes in 'thrown' column each type of exception need.
those few forehead-slapping moments me. care share yours?
two in-code tricks:
i system.diagnostics.debuggerstepthrough attribute; can attach class, method or property make vs not enter code default when debugging. prefer on debuggerhidden attribute still allow put breakpoints in ignored code if need debug it.
another (sometimes) useful call system.diagnostics.debugger.launch(); when execution hits it, presented "select debugger" dialog, , debugger start. bit rude, useful particularly nasty attach processes, process gets spawned , executes code.
Comments
Post a Comment