xxbbcc
xxbbcc

Reputation: 17346

Formatting a 3rd-party .NET object in debugger

I'm working with a 3rd-party library that builds a NameObjectCollectionBase-derived map of name-object pairs. The collection is sometimes big (thousands of items) and I'd like to use the VS 2008 debugger to inspect items in it. (I'm not looking for a specific item in the map, I'm trying to see if the list of items looks like what I expect.) The map contains NameObjectEntry instances. When I inspect this map in the debugger, this is what I see:

[0] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry}
[1] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry}
...
[191] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry}
[192] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry}

I'm wondering if there's a way to tell the debugger how to format NameObjectEntry to show me the data inside an instance, not the type name. Something like:

[0] {"key1","value1"}
[1] {"key2","value2"}
...
[191] {"key191","value191"}
[192] {"key192","value192"}

With my own classes I could either override ToString() or use the DebuggerDisplay attribute but NameObjectEntry is part of the .NET framework, I can't change it. Unfortunately, search results mostly recommend one of these two.

Is there anything I can do to force the debugger to show this object in a specific way?

Edit: for future readers, the solution was to use the autoexp.cs file that comes with VS, as described here:

http://msdn.microsoft.com/en-us/library/x810d419%28VS.90%29.aspx

Upvotes: 3

Views: 245

Answers (1)

Lukasz M
Lukasz M

Reputation: 5723

Yes, you can use DebuggerDisplay attribute. It's easy when you have access to source code of the class. Check this link for more information: http://msdn.microsoft.com/en-us/library/ms228992.aspx.

If you don't have access to the source code, you can try to do the following:

  • make sure you can create classed derived from the class you want to format in debugger
  • create derived class in your own code (it can be just an empty class)
  • apply the DebuggerDisplay attribute to the derived class and you this class in your application (instead of the external one)

So, let's say you have an external class named SampleExternalClass

In your own code, you should create a derived class:

[DebuggerDisplay("custom formatted object with string = { StringPropertyNameFromBaseClass } ")]
class SampleInternalClass : SampleExternalClass
{
    //you don't have to put anything in here
}

After those steps, when you use view object of SampleInternalClass type in your the debugger, it should be formatted as specified in the attribute.

This seems to work when both classes are in the same dll, but I suppose it should work in case of separate dlls as well.

Upvotes: 2

Related Questions