Rediscovered the Debug attributes

There are 2 attributes that can easily be applied to your classes / methods to modify the behavior at debugging time. The attributes don’t change the runtime behavior of your code.

[DebuggerDisplay]

This attribute defines how you’ll see a type or member in the debugger window (like the quick preview, Shift-F9).

There are 3 possibilities:

  1. This attribute is not applied, and the class doesn’t implement ToString( ). In this case the type of the object is shown (which is the default behavior of Object.ToString( ) ).
  2. This attribute is not applied, but the class implements ToString( ). The return value of ToString( ) is shown. Make sure that ToString( ) doesn’t change your object data, or that it doesn’t take ages to execute.
  3. The attribute is applied. Even when ToString( ) is implemented the DebuggerDisplay attribute will be applied.

Example:

[DebuggerDisplay(“Context= {_ctx.Brand}, {_ctx.CountryCode}”]
class SuffixSync
{
    private readonly MyContext _ctx = MyContext.GetContext();

    [ … ]

}

image

[DebuggerBrowsable]

This attribute determines if and how a field or property is displayed in the debugger variable windows.

This example will prevent the debugger to show the e_gateway field:

class SuffixSync
{
    private readonly MyContext _ctx = MyContext.GetContext();
    private readonly IMediator _mediator;
  
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private readonly Gateway _gateway = new Gateway();

image

Other values for the enumeration are

  • DebuggerBrowsableState.Collapsed: shows the element as collapsed.
  • DebuggerBrowsableState.RootHidden: indicates that the member itself is not shown, but its constituent objects are displayed if it is an array or collection.

Reference:

http://msdn.microsoft.com/en-us/library/ms228992(VS.90).aspx

Advertisement

About Gaston

MCT, MCSD, MCDBA, MCSE, MS Specialist
This entry was posted in .Net, Debugging, Development and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s