DebuggerDisplay的泛型类 [英] DebuggerDisplay on generic class

查看:116
本文介绍了DebuggerDisplay的泛型类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用 DebuggerDisplay 属性在泛型类的问题:

I have a problem applying the DebuggerDisplay attribute on a generic class:

[DebuggerDisplay("--foo--")]
class Foo
{
}

[DebuggerDisplay("Bar: {t}")]
class Bar<T>
{
    public T t;
}

当检查类型的对象酒吧&LT;富&GT; 我希望它显示为酒吧:--foo这样 - ,但我得到律师:{美孚}

When inspecting an object of type Bar<Foo> I would expect it to show as Bar: --foo--, but I get Bar: {Foo}

我是什么做错了吗?

推荐答案

该DebuggerDisplay属性不是递归。在{}里面的字符串基本上说,评估该前pression并显示结果行内。该字符串内部结果的计算方法,如果有在玩没有DebuggerDisplay属性类型或成员。这就是为什么你看到的,而不是--foo-- {}美孚。

The DebuggerDisplay attribute is not recursive. The {} inside the string essentially say evaluate this expression and display the result inline. The string for the inner result is calculated as if there was no DebuggerDisplay attribute in play for type or member. That is why you see {Foo} instead of --foo--.

这样做的原因是可靠性。这是太容易有相互递归DebuggerDisplay属性标记。这将导致堆栈溢出或无限循环进行评估时,内部EX pression发生。不递归评估DebuggerDisplay属性prevents这个无限递归(虽然它仍然很可能为用户内部的特定EX pression创建它自己)。

The reason for this is reliability. It is far too easy to have mutually recursive DebuggerDisplay attribute tags. This would cause a stack overflow or infinite loop to occur when evaluating an inner expression. Not recursively evaluating the DebuggerDisplay attribute prevents this infinite recursion (although it's still quite possible for the user to create it themselves inside a particular expression).

您可以控制​​显示内前pression方式的一种方法是通过重写的ToString()方法。这将计算显示字符串内EX pression时进行评估。

One way you can control the way the inner expression is displayed is by overriding the .ToString() method. This will be evaluated when computing the display string for an inner expression.

这篇关于DebuggerDisplay的泛型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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