问题
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