开发者

Attribute that displays property value in Visual Studio

开发者 https://www.devze.com 2023-02-13 12:29 出处:网络
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 thi

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.

0

精彩评论

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

关注公众号