DebuggerTypeProxy - Displaying complex states in the debugger

2/10/2022
3 minute read

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.

How to assert assumptions in code that might not be true?

Sometimes we have to make assumptions in our code. For example, that a certain property or variable has a specific value. On top comes, we have a specific idea but we are not 100% sure if they are correct. Let's meet Debug.Assert.

Entity Framework - Storing complex objects as JSON

From time to time, it is nice to store complex objects or lists as JSON in the database. With Entity Framework 8, this is now easily possible. But this was possible all along with Entity Framework 7.

Questions I asked as .NET interviewer

This blog post is a small collection of questions I asked as a technical interviewer when we had candidates for software developer positions. This might be helpful for you if you are preparing for a job interview.

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