DebuggerBrowsableAttribute

Another debugger attribute is the DebuggerBrowsableAttribute type, which determines how a member is displayed in a debug window. This attribute is valid for properties, indexers, and fields.

DebuggerBrowsableAttribute has a one-argument constructor only. The parameter is the DebuggableBrowsableState enumeration. Table 15-21 lists the elements of the enumeration.

Table 15-21. DebuggableBrowserState values

Value

Description

Never

Hides the member in the Debug window.

Collapsed

Displays the member as collapsed.

RootHidden

Hides the root member but displays the child elements if the element is an array or a collection. For example, when applied to a property that is an integer array, the integer elements are displayed instead of the array itself.

Figure 15-43 shows the values for the ZClass and YClass types without the DebuggerBrowsableAttribute type.

A view of ZClass and YClass types without DebuggerBrowsableAttribute applied

Figure 15-43. A view of ZClass and YClass types without DebuggerBrowsableAttribute applied

The following code shows the ZClass and YClass types decorated with DebuggerBrowsableAttribute, shown in bold. In ZClass, the array members related to the propInts property is displayed. In YClass, fieldb is hidden:

[DebuggerDisplay("NewName")]
class ZClass
{
    public static int test = 1;
    public virtual void MethodA()
    {
        int vara = 5, varb = 10;
        Console.WriteLine("{0} {1}", vara, varb);
    }

    public void MethodB()
    {
        Console.WriteLine("ZClass.MethodB");
        Console.WriteLine("ZClass.fielda {0}",
            fielda);
    }

    private int[] Ints = { 1, 2, 3, 4, 5 };
    [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
    public int[] propInts
    {
        get
        {
            return Ints;
        }
    }

    private int fielda = 5;
}

[DebuggerDisplay("DerivedName")]
class YClass : ZClass
{
    public override void MethodA()
    {
        Console.WriteLine("YClass.MethodA");
        Console.WriteLine("Fieldb: {0}", fieldb);
    }

    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private int fieldb = 10;
}

Figure 15-44 shows the results of using the DebuggerBrowsableAttribute type on ZClass and YClass. Elements of the propInts array are displayed directly and fieldb is no longer displayed.

A view of ZClass and YClass types with DebuggerBrowsableAttribute applied

Figure 15-44. A view of ZClass and YClass types with DebuggerBrowsableAttribute applied

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.227.82