DebuggerTypeProxy - Displaying complex states in the debugger

Let's start with a well known attribute: DebuggerDisplay: DebuggerDisplay controls how an object is displayed in the debugger variable view.

Here a small example:

[DebuggerDisplay("{Name}'s Father: {Father.Name}")]
public class Person
{
    public DateTime Birthday { get; set; }

    public string Name { get; set; }

    public Person Father { get; set; }

    public Person Mother { get; set; }
}

If we debug our application and hover over a Person object we get the following: Display

The DebuggerTypeProxy let's you choose a proxy for your type. That means we will show the proxy instead of the currently watched object. Let's see this in action:

public class PersonDebugView
{
    private string hidden = "I am not visible";
    private Person _person;

    public PersonDebugView(Person person)
    {
        _person = person;
    }

    public string Parents => $"Father: {_person.Father.Name} / Mother: {_person.Mother.Name}";

    public int Age => (DateTime.Now.Year - _person.Birthday.Year);
}

Our proxy takes the Person as an constructor argument. It has two properties and a private string. That is all. Let's see what happens when we attach the debugger:

proxy

In the debug view we can see only the properties of our proxy! And that is all the magic. You can use both attributes together as shown in the picture. To clarify here how both parts are working together:

Both

If you want to play around yourself, I hosted the code on github as gist.

0
An error has occurred. This application may no longer respond until reloaded. Reload x