在调试器中正确显示列表的子类
Display Subclass of List correctly in Debugger
在我的程序中,我写了一个 List
的子类,这里称为 BetterList
。
调试器显示如下:
我可以覆盖某些内容以便 BetterList
正确显示吗?
假设新的 class 公开了 Count
属性,您可以将 DebuggerDisplayAttribute
添加到 class 以显示 属性调试时
[DebuggerDisplay("Count = {Count}")]
public class BetterList: List<SomeType> {
//...
public int Count { get; }
}
对于更复杂的显示场景查看 Using DebuggerTypeProxy Attribute
DebuggerTypeProxyAttribute
specifies a proxy, or stand-in, for a type and changes the way the type is displayed in debugger windows. When you view a variable that has a proxy, the proxy stands in for the original type in the display. The debugger variable window displays only the public members of the proxy type. Private members are not displayed.
[DebuggerDisplay("Count = {Count}")]
[DebuggerTypeProxy(typeof(BetterListDebuggerView))]
public class BetterList: List<SomeType> {
//...
public int Count { get; }
internal class BetterListDebuggerView {
private BetterList list;
public BetterListDebuggerView(BetterList list) {
this.list = list;
}
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public SomeType[] Items {
get {
return list.ToArray();
}
}
}
}
对于这种情况,您应该使用 DebuggerDisplayAttribute
。
查看 Enhancing Debugging with the Debugger Display Attribute 文档以了解有关它的更多详细信息以及一些具有更丰富功能的类似属性。
在我的程序中,我写了一个 List
的子类,这里称为 BetterList
。
调试器显示如下:
我可以覆盖某些内容以便 BetterList
正确显示吗?
假设新的 class 公开了 Count
属性,您可以将 DebuggerDisplayAttribute
添加到 class 以显示 属性调试时
[DebuggerDisplay("Count = {Count}")]
public class BetterList: List<SomeType> {
//...
public int Count { get; }
}
对于更复杂的显示场景查看 Using DebuggerTypeProxy Attribute
DebuggerTypeProxyAttribute
specifies a proxy, or stand-in, for a type and changes the way the type is displayed in debugger windows. When you view a variable that has a proxy, the proxy stands in for the original type in the display. The debugger variable window displays only the public members of the proxy type. Private members are not displayed.
[DebuggerDisplay("Count = {Count}")]
[DebuggerTypeProxy(typeof(BetterListDebuggerView))]
public class BetterList: List<SomeType> {
//...
public int Count { get; }
internal class BetterListDebuggerView {
private BetterList list;
public BetterListDebuggerView(BetterList list) {
this.list = list;
}
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public SomeType[] Items {
get {
return list.ToArray();
}
}
}
}
对于这种情况,您应该使用 DebuggerDisplayAttribute
。
查看 Enhancing Debugging with the Debugger Display Attribute 文档以了解有关它的更多详细信息以及一些具有更丰富功能的类似属性。