DebuggerTypeProxy - Displaying complex states in the debugger

10/02/2022

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.

A replacement for BinaryFormatter in .NET 8

In the old .NET Framework days, you could use the BinaryFormatter class to serialize and deserialize objects. This can be convenient for cloning or storing some session states. As the BinaryFormatter has some serious security concerns, the .NET team marked it as obsolete (as error) in .NET 7 and onwards.

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