I'm Keyvan Nayyeri, a 25 years old Ph.D. student at
the Computer Science department of
the University of Texas at San Antonio.
I'm also
a Software Architect and Developer and previously held a B.Sc.
degree in Applied Mathematics.
This is my blog where I publish content about various topics specifically Programming Languages and Compilers, Software
Engineering, and Programming.
Fahrenheit Marketing is a top-dog Austin Web Design firm offering a complete portfolio of online services.
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.
The output is as simple as what you guessed:
Without a DebuggerTypeProxy attribute I have another Watch Window which doesn't look nice:
Fahrenheit Marketing is your resource for Search Engine Optimization in Austin.
you've been HAACKED
Oct 04, 2006 9:38 AM
#
Keyvan Nayyeri
Oct 04, 2006 9:56 AM
#
Leave a Comment