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:
NewA
's type proxy inherits from that of A
. Properties don't show side-by-side; base properties are umbrella'd [sic] under Base
. *****
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:
... 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:
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):
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:
Still doesn't work. Base properties are not placed side-by-side with the new properties.
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;
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With