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:
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
return this.myStudent.Age;
public String Points
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.Diagnostics;
[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;
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.
The output is as simple as what you guessed:
Without a DebuggerTypeProxy attribute I have another Watch Window which doesn't look nice:
Original Post: Simpler Debugger with DebuggerTypeProxy
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.