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

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

问题描述

我有这从列表与LT继承的类; MagicBean> 。它运作良好,并预期在各方面除了一:当我加入 [DebuggerDisplay] 属性。即使看着列表中包含了其作为 [DebuggerDisplay(COUNT = {计数})] ,如果我这么多的复制和粘贴到我的,我失去的能力直接在所有MagicBeans我有看不钻入基地 - > 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();}}
}

然后,您可以声明这个类进行调试器显示类,用<$ C $一起使用C> 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>
{}

这会给你计数= 3的消息,当您将鼠标悬停在在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天全站免登陆