How to get ToString() to show up in Debug

前端 未结 7 1074
情话喂你
情话喂你 2020-12-15 21:34

I\'d like to get ToString() to display for a class under my control in debug mode.

It\'d be nice if this was the first thing to show up when you hover over a varia

7条回答
  •  北海茫月
    2020-12-15 22:00

    I had a similar issue. My class had an ToString() override and it still didn't show up in VS, which was odd.

    Adding the attribute [System.Diagnostics.DebuggerDisplay("{ToString()}")] to the class showed an exception in the visual studio debugger, where the ToString should have displayed. Turned out I had a bug with incorrectly using string.Format() in my implementation. This is an interesting behavior - VS reverts to the default ToString in case of an exception. The usage of the mentioned attribute forces the display to show the method's output - valid or exception. This is very useful for debugging ToString(). Otherwise there is no point in adding this attribute explicitly to each class since classes have it turned on by default, unless one wants to turn this behavior off for some reason.

提交回复
热议问题