Simpler Debugger with DebuggerTypeProxy

By default Visual Studio nests object properties in Watch Window and shows them in a hierarchy manner.  For more complex objects you need to traverse through your properties to find whatever you like.  This can be a pain when there are several unimportant properties for an object.

But there is a simple way to see what you need in Watch Window.  In order to be able to retrieve your desired properties with a pattern you like from an object in debugger, you only need to declare a class which has some properties (those that you're interested to watch).  This class must have a constructor that gets an object of same type of the class whose values are going to be monitored.  Once you wrote this class, will be able to declare a DebuggerTypeProxy attribute on your class definition which gets the type of your class as parameter.

Here is an example.  I have a Student class which has three properties:

Now I want to simplify what I see for Points property in Watch Window so create a new internal class as follows:

using System;

using System.Collections.Generic;

using System.Text;

 

namespace DebuggerTypeProxySample

{

    internal class MyDebuggerTypeProxy

    {

        private Student myStudent;

 

        public MyDebuggerTypeProxy(Student student)

        {

            this.myStudent = student;

        }

 

        public String Name

        {

            get

            {

                return this.myStudent.Name;

            }

        }

 

        public int Age

        {

            get

            {

                return this.myStudent.Age;

            }

        }

 

        public String Points

        {

            get

            {

                return String.Join(" | ",

                    (String[])this.myStudent.Points.ConvertAll

                    (new Converter<Double, String>(delegate(Double value)

                    {

                        return Convert.ToString(value);

                    }

                    )).ToArray());

            }

        }

    }

}

 

Now I come back to my Student class and put a DebuggerTypeProxy attribute for it:

using System;

using System.Collections.Generic;

using System.Text;

using System.Diagnostics;

 

namespace DebuggerTypeProxySample

{

    [DebuggerTypeProxy(typeof(MyDebuggerTypeProxy))]

    public class Student

    {

        public Student(String name)

        {

            this.Name = name;

        }

 

        private string name;

 

        public string Name

        {

            get { return name; }

            set { name = value; }

        }

 

        private int age;

 

        public int Age

        {

            get { return age; }

            set { age = value; }

        }

 

 

        private List<double> points = new List<double>();

 

        public List<double> Points

        {

            get { return points; }

            set { points = value; }

        }   

    }

}

In Click event handler of a button, I put a simple logic to create a new instance of my Student object and set some initial properties for it:

private void btnDebug_Click(object sender, EventArgs e)

{

    Student myObject = new Student("Gholi");

 

    myObject.Age = 19;

 

    myObject.Points.Add(20);

    myObject.Points.Add(10.25);

    myObject.Points.Add(8.85);

 

    MessageBox.Show("Finished!");

}

Now I put a Breakpoint to monitor myObject properties.

Insert Breakpoint

The output is as simple as what you guessed:

Watch Window

Without a DebuggerTypeProxy attribute I have another Watch Window which doesn't look nice:

Normal Watch Window

[advertisement] Axosoft OnTime 2008 is four developer tools in one: bug tracking, project wiki, feature management, and help desk. It manages your development process so developers can focus on coding. Installed or Hosted – Free Single-user license -- Free 30-day team trial.

2 Comments : 10.04.06

Feedbacks

 avatar
#1
you've been HAACKED
10.04.2006 @ 9:38 AM
Change The Display Of A Type In The Debugger
 avatar
#2
Keyvan Nayyeri
10.04.2006 @ 9:56 AM
Notice the usage of List<T>.ConvertAll() method in my previous post

Leave a Comment