How can I have my DebuggerTypeProxy target class inherit from base proxies?
Asked Answered
C

1

7

Question: I'm looking for a way to simplify the construction of debugger type proxies for inherited classes. So, when debugging a class that inherits from another, I should see the properties of both side-by-side: the base properties of the base class, plus the new properties of the parent class.

Here's what I've tried so far:

  1. NewA's type proxy inherits from that of A. Properties don't show side-by-side; base properties are umbrella'd [sic] under Base. *****
  2. Including an A property in NewA that just casts the current NewA to A, with [DebuggerBrowsable(RootHidden)]: Visual Studio hangs :(

I know that I could just add properties for the base class into NewA's proxy, but I'm trying to avoid this. It's too much work for classes with many properties.


Explanation:

I'm using the DebuggerTypeProxy attribute on some of my classes so I can control how the class looks when browsed during debugging. For example:

public class A {
    private String _someField;

    public String SomeField {
        get {return _someField;}
    }
} 

By default, the tooltip debugging info shows as:

enter image description here

... so I use a DebuggerTypeProxy to hide the backing field:

[DebuggerTypeProxy(typeof(AProxy))]
public class A {
    // ...

    internal class AProxy {
        A _a;
        AProxy (A a){
            _a = a;
        }

        public String SomeField {
             get {return _a.SomeField;}
        }
    }
}

... all is right with the world:

enter image description here


Now, I create a class that inherits from A.

public class NewA : A {
    private String _anotherField;
    public String AnotherField {
        get {return _anotherField;}
    }
}

Unfortunately, when debugging this class, Visual Studio uses the base type proxy (from A). This means we can see the base SomeField property, but our new AnotherField property is hidden (unless you expand Raw View, of course):

enter image description here

Removing the type proxy from base A results in AnotherField showing, but not SomeField.


* Failed attempt #1

/// <summary>
/// The base class
/// </summary>
[DebuggerTypeProxy(typeof(AProxy))]
public class A {
    private String _someField;

    public String SomeField {
        get { return _someField; }
    }

    protected class AProxy {
        A _a;
        protected AProxy(A a) {
            _a = a;
        }

        String SomeField {
            get { return _a.SomeField; }
        }
    }

}

/// <summary>
/// Parent class
/// </summary>
[DebuggerTypeProxy(typeof(NewAProxy))]
public class NewA : A {
    private String _anotherField;
    public String AnotherField {
        get { return _anotherField; }
    }

    // Inherit base type proxy, in an effort to display base properties
    // side-by-side with AnotherField: Doesn't work.
    protected class NewAProxy : A.AProxy {
        NewA _newA;
        protected NewAProxy(NewA newA)
            : base(newA) {
            _newA = newA;
        }

        public String AnotherField {
            get { return _newA.AnotherField; }
        }
    }
}

Result:

enter image description here

Still doesn't work. Base properties are not placed side-by-side with the new properties.

Cinematograph answered 16/9, 2012 at 0:26 Comment(0)
C
6

After hours of searching and tinkering, I found the solution - and a beautiful one at that - from Jared Par's blog. He creates a type proxy that uses reflection to condense all the members into one list. Some additional DebuggerDisplay magic makes it so you don't even notice.

// http://blogs.msdn.com/b/jaredpar/archive/2010/02/19/flattening-class-hierarchies-when-debugging-c.aspx
// by Jared Par
internal sealed class FlattenHierarchyProxy {

    [DebuggerDisplay("{Value}", Name = "{Name,nq}", Type = "{Type.ToString(),nq}")]
    internal struct Member {
        internal string Name;
        internal object Value;
        internal Type Type;
        internal Member(string name, object value, Type type) {
            Name = name;
            Value = value;
            Type = type;
        }
    }

    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private readonly object _target;
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private Member[] _memberList;

    [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
    internal Member[] Items {
        get {
            if (_memberList == null) {
                _memberList = BuildMemberList().ToArray();
            }
            return _memberList;
        }
    }

    public FlattenHierarchyProxy(object target) {
        _target = target;
    }

    private List<Member> BuildMemberList() {
        var list = new List<Member>();
        if ( _target == null ) {
            return list;
        }

        var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
        var type = _target.GetType();
        foreach (var field in type.GetFields(flags)) {
            var value = field.GetValue(_target);
            list.Add(new Member(field.Name, value, field.FieldType));
        }

        foreach (var prop in type.GetProperties(flags)) {
            object value = null;
            try {
                value = prop.GetValue(_target, null);
            }
            catch (Exception ex) {
                value = ex;
            }
            list.Add(new Member(prop.Name, value, prop.PropertyType));
        }

        return list;
    }
}


Modifications

I made three little modifications to the class to make it more usable for me.

First, I wanted the members sorted by name. To do this, change the last line to:

return list.OrderBy(m => m.Name).ToList();

Second, in the Member struct, I added some attributes so that only the value would show when you expand a reference class:

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal string Name;
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
internal object Value;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal Type Type;

Third, the default flags BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance mean that even members marked [DebuggerBrowsable(DebuggerBrowsableState.Never)] will still be shown. To prevent this from happening, after this line:

foreach (var field in type.GetFields(flags)) {

add this:

// Respect DebuggerBrowsableAttributes
var debuggerBrowsableAtts = field.GetCustomAttributes(typeof(DebuggerBrowsableAttribute), true);
if (debuggerBrowsableAtts.Count() == 1) {
    var att = debuggerBrowsableAtts[0] as DebuggerBrowsableAttribute;
    if (att.State == DebuggerBrowsableState.Never) {
        continue;
    }
}

Now the DebuggerBrowsable(DebuggerBrowsableState.Never) will be respected for fields. You can also add that code the foreach loop that handles properties, to have it respected for properties as well.

Cinematograph answered 16/9, 2012 at 2:56 Comment(1)
you can also enable DebuggerBrowsableAttributes for the properties.Slowwitted

© 2022 - 2024 — McMap. All rights reserved.