开发者

How to get ToString() to show up in Debug

开发者 https://www.devze.com 2023-03-23 12:14 出处:网络
I\'d like to get ToString() to display for a class under 开发者_C百科my control in debug mode. It\'d be nice if this was the first thing to show up when you hover over a variable with the mouse.Is th

I'd like to get ToString() to display for a class under 开发者_C百科my control in debug mode.

It'd be nice if this was the first thing to show up when you hover over a variable with the mouse. Is there an attribute for this?


Mark your class with

[System.Diagnostics.DebuggerDisplay("{ToString()}")]

Test:

[System.Diagnostics.DebuggerDisplay("{ToString()}")]
class MyClass
{
    private string _foo = "This is the text that will be displayed at debugging"

    public override string ToString()
    {
        return _foo;
    }
}

Now when you hover over a variable with the mouse it will show This is the text that will be displayed at debugging.


There is DebuggerDisplayAttribute which lets you influence the display. It allows you to write fairly complex expressions to produce the debug output, although it is not recommended to do so.

However, if you have overriden ToString then the debugger is documented to display that by default. Maybe there's something wrong with the code?


The output of ToString should be the default you see when debugging.

It can be overridden using the DebuggerDisplay Attribute (see MSDN).

I prefer overriding the ToString method because its easier and more versatile because it helps when writing to log files as well.

What output do you see? If you get the type name you see the default ToString.


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.


What you are looking for is the DebuggerDisplayAttribute:

http://www.codeproject.com/Articles/117477/Using-DebuggerDisplayAttribute

Use the above link to see how it's done and then apply this to your class, using the ToString() method to drive what is shown. I've only ever used properties, not sure if you can inject classes.


in the object Override the .ToString as follows:

public class MyObject
{
        public int Property1{ get; set; }
        public string Property2{ get; set; }
        public string Property3 { get; set; }

        public override string ToString()
        {
            return Property3;
        }
}

This will return Property3 as the ToString() value


If you are using visual studio you could add a watch @ runtime om the yourvariable.ToString() line, this will show up in the bottom of your screen when it hits a breakpoint


My problem was that although I overrided the ToString() method in a class, the debugger still did not show the ToString() contents. The reason was that the base class already had a DebuggerDisplay attribute and this one took precedence over the ToString() method of the derived class. The solution was to add the DebuggerDisplay attribute to the derived class, too:

[DebuggerDisplay("Name: {Name}")]
class Base
{
    public string Name { get; set; }
}

// Add this line to show the Address instead of Name in the debugger.
[DebuggerDisplay("{ToString()}")]
class Derived : Base
{
    public string Address { get; set; }

    public override string ToString()
    {
        return $"Address: {Address}";
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消