Create a Debugger Visualizer in Visual Studio 2005 for Custom Types and Classes
by David Hayden ( .NET Developer )
Debugger Visualizers Intro and Examples
I have been talking about several debugger visualizers on my blog:
If you haven't had a chance or the need to create a debugger visualizer yourself, they are as simple as creating a Windows Form.
Debugger Visualizer Template for Visual C# 2005
If you develop in C#, Visual C# 2005 has an item template for creating debugger visualizers:

The template does a couple of cool things for you:
- It adds a reference to the following assembly: Microsoft.VisualStudio.DebuggerVisualizers
- Generates a simple class for you that derives from DialogDebuggerVisualizer
Let's use this item template to create a custom ShoppingCart Debugger Visualizer for our E-Commerce Framework.
E-Commerce Framework: ShoppingCart and Item Classes
In our modest ecommerce framework there is a ShoppingCart Class and an Item Class. A ShoppingCart consists of a generic collection of Item Classes and a single property - SubTotal:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace Ecommerce
{
[Serializable]
public class ShoppingCart
{
private List<Item> _items = new List<Item>();
public List<Item> Items
{
get { return _items; }
}
public decimal SubTotal
{
get { return CalculateSubTotal(); }
}
public ShoppingCart()
{
}
public void AddNewItem(string code, string title,
int quantity, decimal price)
{
_items.Add(new Item(code, title, quantity, price));
}
public decimal CalculateSubTotal()
{
decimal total = 0m;
foreach (Item item in _items)
{
total += item.Price;
}
return total;
}
}
}
using System;
namespace Ecommerce
{
[Serializable]
public class Item
{
private string _code = string.Empty;
private string _title = string.Empty;
private int _quantity;
private decimal _price;
public string Code
{
get { return _code; }
set { _code = value; }
}
public string Title
{
get { return _title; }
set { _title = value; }
}
public int Quantity
{
get { return _quantity; }
set { _quantity = value; }
}
public decimal Price
{
get { return _price; }
set { _price = value; }
}
public Item()
{
}
public Item(string code, string title,
int quantity, decimal price)
{
_code = code;
_title = title;
_quantity = quantity;
_price = price;
}
}
}
Debugging Without a Custom Debugger Visualizer
If you happen to be debugging the shopping cart class without a custom debugger visualizer, this is all you can expect from Visual Studio 2005:

Granted for this simple example, we could live with such a basic view of the data, but I would rather create something that looks a bit more ShoppingCart-Like.
ShoppingCartDebuggerVisualizer Class
My ShoppingCartDebuggerVisualizerClass that derives from DialogDebuggerVisualizer is pretty simple as most of the setup work is done in the template and I just need to change a few lines for my custom solution:
using System;
using System.Diagnostics;
using System.Windows.Forms;
using Microsoft.VisualStudio.DebuggerVisualizers;
using Ecommerce;
[assembly: System.Diagnostics.DebuggerVisualizer(
typeof(EcommerceDebugVisualizers.ShoppingCartDebugVisualizer),
Target = typeof(Ecommerce.ShoppingCart),
Description = "ShoppingCart Visualizer")]
namespace EcommerceDebugVisualizers
{
///
/// ShoppingCartDebugVisualizer
///
public class ShoppingCartDebugVisualizer :
DialogDebuggerVisualizer
{
protected override void
Show(IDialogVisualizerService windowService,
IVisualizerObjectProvider objectProvider)
{
ShoppingCart cart =
(ShoppingCart)objectProvider.GetObject();
using (ShoppingCartDebugVisualizerForm displayForm
= new ShoppingCartDebugVisualizerForm())
{
displayForm.Cart = cart;
windowService.ShowDialog(displayForm);
}
}
}
}
Essentially, we just override a single method, Show, which is the single point of entrance into your Debugger Visualizer.
protected override void Show(IDialogVisualizerService
windowService,
IVisualizerObjectProvider
objectProvider)
Within this method, we extract the object being debugged, a ShoppingCart, via the following command:
ShoppingCart cart = (ShoppingCart)objectProvider.GetObject();
And then display a windows form:
using (ShoppingCartDebugVisualizerForm displayForm
= new ShoppingCartDebugVisualizerForm())
{
displayForm.Cart = cart;
windowService.ShowDialog(displayForm);
}
The ShoppingCartDebugVisualizerForm consists of a DataGridView to display the collection of items and a label to display the ShoppingCart subtotal.
private void ShoppingCartDebugVisualizerForm_Load
(object sender, EventArgs e)
{
dataGridView1.DataSource = _cart.Items;
SubTotal.Text = String.Format("SubTotal: {0}",
_cart.SubTotal.ToString("c"));
}
After we build and deploy the debugger visualizer by placing it in the My Documents > Visual Studio 2005 > Visualizers Folder, you now have the following option when debugging a ShoppingCart Class:

Displaying the ShoppingCart Class now provides a more interesting view that makes it easier to debug:

Conclusion
Although the concept of a Debugger Visualizer seems intimidating, Visual Studio 2005 has made debugging and deploying them a breeze. Visual C# 2005 has an item template that stubs out most of the class for you, and you can certainly create a similar item template for VB.NET or just add the information manually. Either way, creating a debugger visualizer is pretty simple and may be worthwhile to create for certain classes in your frameworks that you plan to use for quite some time.
Written by David Hayden ( .NET Developer )
Related Posts: