我正在寻找一种方法来简化继承类的调试器类型代理的构造.因此,在调试从另一个继承的类时,我应该看到并排的属性:基类的基本属性,以及父类的新属性.
这是我到目前为止所尝试的内容:
> NewA的类型代理继承自A的属性.属性不并排显示;基础属性是在Base下的[sic]. *****
>在NewA中包含一个A属性,只使用[DebuggerBrowsable(RootHidden)]将当前NewA强制转换为A:Visual Studio挂起:(
我知道我可以将基类的属性添加到NewA的代理中,但我试图避免这种情况.这对于具有许多属性的类来说太过分了.
说明:
我在我的一些类上使用DebuggerTypeProxy属性,因此我可以控制在调试期间浏览时类的外观.例如:
public class A { private String _someField; public String SomeField { get {return _someField;} } }
…所以我使用DebuggerTypeProxy来隐藏支持字段:
[DebuggerTypeProxy(typeof(AProxy))] public class A { // ... internal class AProxy { A _a; AProxy (A a){ _a = a; } public String SomeField { get {return _a.SomeField;} } } }
……一切都与世界是对的:
现在,我创建了一个继承自A的类.
public class NewA : A { private String _anotherField; public String AnotherField { get {return _anotherField;} } }
不幸的是,在调试此类时,Visual Studio使用基类型代理(来自A).这意味着我们可以看到基本的SomeField属性,但我们新的AnotherField属性是隐藏的(除非你扩展Raw View,当然):
从基础A中删除类型代理会导致显示AnotherField,但不会显示SomeField.
*尝试失败#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; } } } }
结果:
解决方法
// 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,prop.PropertyType)); } return list; } }
我对课程进行了三次小修改,使其对我更有用.
首先,我希望成员按名称排序.为此,请将最后一行更改为:
return list.OrderBy(m => m.Name).ToList();
其次,在Member结构中,我添加了一些属性,以便在展开引用类时只显示该值:
[DebuggerBrowsable(DebuggerBrowsableState.Never)] internal string Name; [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] internal object Value; [DebuggerBrowsable(DebuggerBrowsableState.Never)] internal Type Type;
三,默认标志BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance意味着仍然会显示标记为[DebuggerBrowsable(DebuggerBrowsableState.Never)]的成员.为防止这种情况发生,请在此行之后:
foreach (var field in type.GetFields(flags)) {
添加这个:
// 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; } }
现在,对于字段,将遵守DebuggerBrowsable(DebuggerBrowsableState.Never).您还可以将该代码添加到处理属性的foreach循环中,以使其也适用于属性.