Paragon
Paragon

Reputation: 119

DebuggerDisplay ignored when showing collection of items

Lets say I have an object:

[DebuggerDisplay("Bar={bar}")]
public class Foo
{
    public String bar{get;set;}
}

When I have a single instance of bar the debugger correctly shows Bar="value of bar" but when I have a dictionary of Foos, the dictionary shows:

{[key, namespace.Foo]}

when I expand the kvp I get the expected debugger display string.

When I override ToString() in Foo and have a dictionary of Foo the dictionary shows:

{[key, Bar="value of bar"]}

According to the documentation, ToString() is only used for debugger display if a DebuggerDisplay Attribute doesn't override it...

How do I get the debuggerDisplay attribute to override the debugger string in enumerated situations in addition to single instances?

Upvotes: 2

Views: 1242

Answers (1)

hansfbaier
hansfbaier

Reputation: 639

An elegant solution to this problem is to apply the DebuggerDisplay attribute to System.Collections.Generic.KeyValuePair<TKey,TValue> via AssemblyInfo.cs as follows:

using System.Collections.Generic;
using System.Diagnostics;

[assembly: DebuggerDisplay("[Key={Key}, Value={Value}]", Target = typeof(KeyValuePair<,>))]

(Source on StackoverFlow)

Upvotes: 1

Related Questions