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

ぐ巨炮叔叔 提交于 2020-01-24 08:50:08

问题


Overriding a class's ToString() is usually all you need to do to get custom formatting in the watch window, but when the class is derived from a list it doesn't seem to work.

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

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

Inspecting 'test' in the watch window I get

ListOfInts.test             Count = 3   ListOfInts

and have to manually force the issue like so:

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

This is fine for a single list, but I have a large array of the things. Is there a way to stop the default "Count = 3" format from taking priority?


回答1:


You can use the DebuggerDisplayAttribute:

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


来源:https://stackoverflow.com/questions/40040552/can-the-watch-window-value-of-a-list-be-customised

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!