DebuggerDisplay attribute not working properly

I know this attribute should work in C #, and yet, in my case, it is not. I have a class with the lazy property Kids. Access to this property can have a side effect when accessing the server. So naturally, I don’t want this to happen when I just watch it in the debugger viewport.

Omitting all the irrelevant details, the source looks quite ordinary:

[DebuggerDisplay("(Frozen) {m_children}")] public IList<IEntityBase> Children { get { if (m_children == null) { m_children = FetchChildrenFromDB(this); } return m_children; } } 

And yet, when I observe the object and open this in the viewport, I do not see (frozen) the display, that is, the debugger simply ignores the attribute.

DebuggerDisplay image snapshot

There really is an attribute, according to Reflector. I am using VS2008.

Any ideas?

+4
source share
4 answers

If you see something along the lines in your watch window:

 [+] ObjectName | { namespace.object} 

Make sure that "Tools-> Options-> Debugging-> General-> Show raw structure of objects in the variable windows is NOT checked.

As soon as I cleaned it up, my DebuggerDisplay attributes displayed correctly (including showing all the “WTF” and “Huh” that I added ...)

+5
source

Well, I just tested it and it works with my simple program. I also thought that I have a possible explanation, but testing shows that this is not what I thought (information below the code).

Firstly, here is the code that works:

 using System; using System.Diagnostics; using System.Collections.Generic; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Program p = new Program(); Console.Out.WriteLine(p.Name); // breakpoint here } private String _Name = String.Empty; [DebuggerDisplay("Name: {_Name}")] public String Name { get { return _Name; } set { _Name = value; } } private IList<String> _Names = new List<String>(); [DebuggerDisplay("Names: {_Names.Count}")] public IList<String> Names { get { return _Names; } set { _Names = value; } } } } 

I thought that the collection class that you are extracting from the FetchChildrenFromDB method has its own DebuggerDisplay attribute attached to it, and it takes precedence. But this is not so. I implemented a dummy IList class with an attribute attached to it, and the one that was bound to this property still took precedence.

0
source

I think this may be due to the brackets "(Frozen)".
Change it to Frozen if it is text.

By the way, what is Frozen? Is this plain text or an existing property? EDIT . This is what I guessed based on the sample code in MSDN and Lasse code.

0
source

You should put DebuggerDisplayAttribute in the class and not in the property, because m_children is an instance field and cannot be evaluated in the context of properties.

Property mapping is always evaluated as is, because there is no debugger proxy for it.

0
source

All Articles