Saturday, 15 September 2012

c# - Forcing Visual Studio Debugging Tools to display useful information -



c# - Forcing Visual Studio Debugging Tools to display useful information -

as know, when want @ internal variables of complex objects in visual studio debugger class name in have expand see public properties:

i attempting utilize code reply this question instead of rewriting tostring methods each class.

but doesn't seem create difference. else can try?

you have many options, i'll show them powerful simple one.

custom visualizer

you may take debugger visualizers. can provide own ui debugging classes. thought pretty similar how propertygrid editors works (ivisualobjectprovider , dialogdebuggervisualizer) , may reuse code inspect object property grid. let's see trivial - , untested - illustration (adapted msdn).

public class propertygridinspectorvisualizer : dialogdebuggervisualizer { protected override void show( idialogvisualizerservice windowservice, ivisualizerobjectprovider objectprovider) { var propertygrid = new propertygrid(); propertygrid. dock = dockstyle.fill; propertygrid.selectedobject = objectprovider.getobject(); form form = new form { text = propertygrid.selectedobject.tostring() }; form.controls.add(propertygrid); form.showdialog(); } // other stuff, see msdn }

to utilize custom visualizer need decorate class follow:

[debuggervisualizer(typeof(propertygridinspectorvisualizer))] class dimension { } proxy object

there way have alternative debug view of object: debuggertypeproxyattribute. won't see object you're debugging custom proxy (that can shared across classes , rely on typeconverter). in short it's this:

[debuggertypeproxy(customdebugview)] class dimension { } // debugger show object (calling tostring() method // required , showing properties , fields) public class customdebugview { public customdebugview(object obj) { _obj = obj; } public override string tostring() { // proxy much more powerful because // can expose different view of object // can utilize invoke typeconverter conversions. homecoming _obj.tostring(); // replace true code } private object _obj; } non-invasive tostring() replacement

last way (afaik) utilize debuggerdisplayattribute (msdn details). can handle complex cases it's pretty handy in many situations: build string visualized debugger when inspect object. example:

[debuggerdisplay("architectural dimension = {architectural}")] class dimension { }

c# properties visual-studio-2013 tostring typeconverter

No comments:

Post a Comment