Alan Baljeu
Alan Baljeu

Reputation: 2433

Is there a way to view an object's variables without seeing all the base class stuff In Visual Studio debugger

In particular, examining a Windows Form object, I want to see the members defined in my code without always seeing the hundreds of members defined by the framework in the base class.

enter image description here

Upvotes: 2

Views: 170

Answers (1)

Thomas Flinkow
Thomas Flinkow

Reputation: 5105

There are multiple inbuilt ways using Framework Attributes (though none of which particularly scalable).


1. Using [DebuggerDisplay]

Assume your class looks like this:

public class MyForm : Form
{
    public int Foo { get; set; }
    public string Bar { get; set; }
}

you can add the [DebuggerDisplayAttribute] to the class like this:

[DebuggerDisplay("Foo = {Foo}, Bar = {Bar}")
public class MyForm : Form
{
    public int Foo { get; set; }
    public string Bar { get; set; }
}

which now only shows Foo and Bar.

Advantage: displays only those members you wish to.

Disadvantage: you have to explicitly specify the members to display, and repeat that step for every property you add later.


2. Using [DebuggerTypeProxy]

You can define a proxy class that should be shown in the debugger instead of your real class.

Again, assuming that your class looks like this

public class MyForm : Form
{
    public int Foo { get; set; }
    public string Bar { get; set; }
}

you can now go ahead and define a proxy class that exposes only those members that you want to display and apply the [DebuggerTypeProxyAttribute] to the class like this:

[DebuggerTypeProxy(typeof(MyFormDebugView))]
public class MyForm : Form
{
    public int Foo { get; set; }
    public string Bar { get; set; }
}

public class MyFormDebugView
{
    public int Foo { get; set; }
    public string Bar { get; set; }
}

Now you will only see Foo and Bar in the debugger display, not the inherited members of Form.

Advantage: displays only the members in the proxy class. No need to specify those members explicitly.

Disadvantage: you have to adjust the proxy type whenever you add a new property etc. to your MyForm that should be seen in the debugger.

Upvotes: 5

Related Questions