DebuggerDisplay & DebuggerTypeProxy

There are a few options to make debugging simpler, for example by displaying data in better way. Two of them are explained below.

1. DebuggerDisplay attribute on Employee class decides how variables are presented during debugging. We declare in the attribute a string used to display variable in i.e. watch window.

//the way how variable is presented during debugging

    [DebuggerDisplay("Employee: {Name}, {Age} years old.")]
    class Employee
    {
        public Employee(string name, int age)
        {
            Name = name;
            Age = age;
            Skills = new List<Skill>();
        }

        public string Name { get; set; }
        public int Age { get; set; }
        public List<Skill> Skills { get; set; }
    }

The result of the code above is presented below.
debuggerdisplay0

2. DebuggerTypeProxy attribute allows us to use additional proxy class to decide what is displayed during debugging. Notice that I wrote internal EmployeeDebuggerProxy class which exposes two properties calculated somehow on _employee instance. This proxy class is used in DebuggerTypeProxy attribute on Employee class.

// use proxy class to present data during debugging
    [DebuggerTypeProxy(typeof(EmployeeDebuggerProxy))] 
    class Employee
    {
        public Employee(string name, int age)
        {
            Name = name;
            Age = age;
            Skills = new List<Skill>();
        }

        public string Name { get; set; }
        public int Age { get; set; }
        public List<Skill> Skills { get; set; }

        internal class EmployeeDebuggerProxy
        {
            private Employee _employee;

            public EmployeeDebuggerProxy(Employee e)
            {
                _employee = e;
            }

            ///<summary>
            /// Gets the name and age of the employee.
            /// </summary>
            public string DisplayName
            {
                get
                {
                    return String.Format($"{_employee.Name} ({_employee.Age})");
                }
            }

            ///<summary>
            /// Gets the most important skill of employee.
            /// </summary>
            public string TopSkill
            {
                get
                {
                    return _employee.Skills
                        .OrderByDescending(s => s.YearsOfExperience)
                        .FirstOrDefault().Technology;
                }
            }
        }
    }

    ///<summary>
    /// Helper class to present employee's skills.
    /// </summary>
    class Skill
    {
        public string Technology { get; set; }
        public int YearsOfExperience { get; set; }
    }

The result during debugging.

debuggertypeproxy

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s