adam0101
adam0101

Reputation: 31005

Attribute that displays property value in Visual Studio

I believe I saw somewhere an attribute that, when applied to a class, would show the value of a property in intellisense. I'm not talking about XML comments. It looked something like this:

[SomeAttribute("Name = '{0}', Age = '{1}'", Name, Age)]
MyClass

Anyone know which Attribute I'm talking about?

Upvotes: 5

Views: 636

Answers (2)

adrianbanks
adrianbanks

Reputation: 82944

Are you sure you are not thinking of the DebuggerDisplayAttribute used while debugging? That has a similar format to the one you have shown, but is used to give a "value" to a class for debugging that is shown in the debug window and when hovering the mouse over an instance.

The format is not the same as a string format like you have, but uses a special syntax:

[DebuggerDisplay("Name = '{Name}', Age = '{Age}'")]
MyClass

When debugging, this will show the values of the Name and Age properties of the instance of MyClass in the string instead of the type of MyClass.

Upvotes: 2

Timwi
Timwi

Reputation: 66583

It doesn’t make sense to “show a value in IntelliSense”, but I guess you mean in the debugger. In that case, the attribute you’re looking for is DebuggerDisplayAttribute:

[DebuggerDisplay("Name = '{Name}', Age = '{Age}'")]
public class XYZ
{
    public string Name;
    public int Age;
}

Of course, you can also override the ToString() method instead. In the absense of a DebuggerDisplayAttribute, the debugger uses ToString(). You should use DebuggerDisplayAttribute only if you really need the implementation of ToString() to be different (and insufficient for debugging).

Upvotes: 4

Related Questions