可以自定义 List<> 的 watch window 值吗?

Can the watch window value of a List<> be customised?

覆盖 class 的 ToString() 通常是在手表 window 中获取自定义格式所需要做的全部工作,但是当 class 是从列表派生时好像不行。

class ListOfInts : List<int>
{
    public override string ToString()
    {
        return string.Join(",", this);
    }

    public static ListOfInts test = new ListOfInts() { 3, 4, 5 };
}

在手表中检查'test' window 我得到

ListOfInts.test             Count = 3   ListOfInts

并且必须像这样手动强制执行此问题:

ListOfInts.test.ToString()  "3,4,5"     string

这对于单个列表来说没问题,但我有很多东西。有没有办法阻止默认 "Count = 3" 格式优先?

您可以使用 DebuggerDisplayAttribute:

[DebuggerDisplay("{ToString()}")]
public class ListOfInts : List<int>
{
    public override string ToString()
    {
        return string.Join(",", this);
    }
}