Attribute that displays property value in Visual Studio
I believe I saw somewhe开发者_Go百科re 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?
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).
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
.
精彩评论