3

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

xxbbcc
  • 16,930
  • 5
  • 50
  • 83

1 Answers1

2

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.

Lukasz M
  • 5,635
  • 2
  • 22
  • 29
  • How do I apply this attribute to a type I don't have the source for? – xxbbcc Mar 01 '12 at 20:31
  • Hmmm, sorry, it seems I missed that it's 3rd party dll and I thought you have access to the source of the class. I'll look for another solution to work in your case. – Lukasz M Mar 01 '12 at 20:52
  • Thanks, but I don't think it helps me: I don't create these objects, I get them from a 3rd-party library. I can create a derived class of the type in question but the 3rd-party library won't use my derived class - it'll use the original class. – xxbbcc Mar 01 '12 at 21:16
  • You could convert objects to your class (build new objects) based on the original ones, but this will probably decrease overall app's performance. The solution I described is rather for debugging cases, when you want to detect bugs quickly and should be then removed from the code. – Lukasz M Mar 01 '12 at 21:54
  • I'd like to avoid that - that's why I asked the question about telling the debugger to format the data on the fly. If I have to go through all that in a bunch of places, it doesn't really help me. Thanks anyway. – xxbbcc Mar 01 '12 at 21:56
  • You can take a look here: http://www.servicestack.net/mythz_blog/?p=202 and here: http://code.google.com/p/linqpadvisualizer/, I haven't tested those, but maybe they'll work for you. – Lukasz M Mar 01 '12 at 22:30
  • Thanks, I'll take a look at that. It's not exactly what I'm looking for but if I don't get any more (more helpful) answers, I'll mark yours as accepted because I really appreciate you trying to help. – xxbbcc Mar 02 '12 at 04:05