如何使[DebuggerDisplay]尊重继承的类或至少与集合一起工作? [英] How to make [DebuggerDisplay] respect inherited classes or at least work with collections?

查看:266
本文介绍了如何使[DebuggerDisplay]尊重继承的类或至少与集合一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个继承自 List&MagicBean> 的类。它除了一个以外,在所有方面都可以正常工作:当我添加 [DebuggerDisplay] 属性时。即使看到列表,它的作为 [DebuggerDisplay(Count = {Count})] ,如果我如此复制并粘贴到我的,我失去了能力直接查看所有的MagicBean,我在调试时没有钻入base-> private成员。

I've got a class which inherits from a List<MagicBean>. It works well and as expected in all respects except one: when I add the [DebuggerDisplay] attribute. Even though looking at List has its as [DebuggerDisplay("Count = {Count}")], if I so much as copy and paste that onto mine, I lose the ability to look directly at all of the MagicBeans I have without drilling into base->private members while debugging.

我如何获得两个世界的最佳效果? IE:值列中的自定义值,Visual Studio不会隐藏我的魔术豆?

How do I get the best of both worlds? IE: Custom value in the value column, and Visual Studio not hiding my magic beans from me?

推荐答案

您可以获得效果您需要使用 DebuggerTypeProxy 属性。您需要创建一个类来对继承的列表进行调试可视化:

You can get the effect you need by using the DebuggerTypeProxy attribute. You need to create a class to make a debug "visualisation" of your inherited list:

internal sealed class MagicBeanListDebugView
{
    private List<MagicBean> list;

    public MagicBeanListDebugView(List<MagicBean> list)
    {
        this.list = list;
    }

    [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
    public MagicBean[] Items{get {return list.ToArray();}}
}

然后,您可以声明此类被调试器用于显示您的课程,以及 DebuggerDisplay 属性:

You can then declare this class to be used by the debugger for displaying your class, along with the DebuggerDisplay attribute:

[DebuggerDisplay("Count = {Count}")]
[DebuggerTypeProxy(typeof(MagicBeanListDebugView))]
public class MagicBeanList : List<MagicBean>
{}

当您将鼠标悬停在您在Visual Studio中继承的列表的实例,以及展开根节点时列表中项目的视图,而无需深入到基础属性中。

This will give you the "Count = 3" message when you hover over an instance of your inherited list in Visual Studio, and a view of the items in the list when you expand the root node, without having to drill down into the base properties.

使用 ToString()专门获取调试输出不是一个好办法,除非你已经覆盖了 ToString()在其他地方的代码中使用,在这种情况下,您可以使用它。

Using ToString() to specifically get debug output is not a good approach, unless of course you are already overriding ToString() for use in your code elsewhere, in which case you can make use of it.

这篇关于如何使[DebuggerDisplay]尊重继承的类或至少与集合一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆