CSharpFeeds - All your C# feeds in one place.

Sponsors

Wednesday, October 04, 2006

Simpler Debugger with DebuggerTypeProxy

by Keyvan Nayyeri via Keyvan Nayyeri on 10/4/2006 1:30:15 PM

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:

  • Name: String value of student's name.
  • Age: Integer value of student's age.
  • Points: List<double> of student's points.

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

email it!bookmark it!digg it!

Original Post: Simpler Debugger with DebuggerTypeProxy

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us