DebuggerDisplay的泛型类DebuggerDisplay、泛型类

2023-09-03 06:30:55 作者:阿某i

我的应用 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.