4

I know that this attribute should work in C# and yet, in my case it does not. I have a class with a lazy property Children. Accessing this property may have a side effect of roundtripping to the server. So, naturally, I do not want this to happen when I just watch it in the debugger watch window.

Omitting all the irrelevant details the source looks pretty 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 watch the object and expand this in the watch window I do not see (Frozen) in the display, meaning the debugger simply ignores the attribute.

DebuggerDisplay image snapshot

The attribute is really there, according to Reflector. I use VS2008.

Any ideas?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
mark
  • 59,016
  • 79
  • 296
  • 580
  • Have you checked that you haven't inadvertently introduced your own DebuggerDisplay attribute, perhaps for some debugging issues? – Lasse V. Karlsen Aug 19 '09 at 19:42
  • 1
    If this is part of some ORM-implementation, could it be that the ORM-framework has created a shim-layer around your actual object, to implement observable properties? I've seen those things happen, and in that case, the type of the objects doesn't match your source code. Can you check that you're looking at the exact type you have in the source code? – Lasse V. Karlsen Aug 19 '09 at 19:47
  • The code is a client side code. No ORM is used at that side. We do use NHibernate on the server side, but the client side is completely detached from it. I am sure it is not a shim. – mark Aug 19 '09 at 20:09

4 Answers4

5

If you are seeing in your watch window something along the lines of:

[+]  ObjectName    | { namespace.object}

Ensure that "Tools->Options->Debugging->General->Show raw structure of objects in variables windows" is NOT checked.

Once I cleared this, my DebuggerDisplay attributes displayed correctly (including showing all the "WTF"'s and "Huh"'s I'd added...)

PaulS
  • 699
  • 7
  • 13
1

Well, I just tested it and it works with my simple program. I also thought I had a possible explanation, but testing shows that it wasn't what I thought (info below code).

First, here's 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; }
        }
    }
}

What I thought was that the collection class that you retrieve from FetchChildrenFromDB method had its own DebuggerDisplay attribute attached to it, and it took priority. But that's not it. I implemented a dummy IList class with that attribute attached to it, and the one attached to the property still took priority.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • I know that DebuggerDisplay should work. That's why I am puzzled. It clearly does not work for my lazy property. – mark Aug 19 '09 at 20:00
0

I think, it could be due to brackets "(Frozen)".
Change it to "Frozen", if it is text.

BTW, what is "Frozen"? Is it a simple text or an existing property?
EDIT: This is what I guessed based on the example code on MSDN & Lasse's code.

shahkalpesh
  • 33,172
  • 3
  • 63
  • 88
  • Frozen is just a word to let me know that the attribute works. You can replace it with any other word, omit the brackets, whatever. Nothing helps. – mark Aug 19 '09 at 20:05
0

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

The property display is always evaluated as is, because there is no debugger proxy for it.

Laurent Etiemble
  • 27,111
  • 5
  • 56
  • 81