April 2007 - Posts

Debugging: Type Visualizers

Well it's been a while since I've last updated my blog, there is lots going on. No sooner had I finished what has been the biggest project I've been involved in when I was handed another project that gave me a chance to look into .NET v3.0 Workflow Foundation. That as well as studying for the MCAD -> MCPD Windows upgrade exam has taken up most of my time.

Today I covered the new debugging and tracing classes and found this easy, yet very powerful, way to extend the debugging capabilities of Visual Studio using something called Type Visualizers. Your all familar with type visualizers, if you've debugged an application that uses a DataSet you'll have used them. When you look at a DataSet in the debugging windows in VS you'll see a small magnifying glass that when you click it gives you a way to view the DataSet in a grid. This is a Type Visualizer and here's how you go about creating your own, it's real simple.

1. Create a class library and add a reference to the Microsoft.VisualStudio.DebuggerVisualizers assembly.

2. Create a class, take the example of a Bitmap visualizer, and inherit the DialogDebuggerVisualizer. Override the Show() method. This method accepts two interfaces, you'll use one interface (IDialogVisualizerService) to display a modal dialog/form while the other interface (IVisualizerObjectProvider) is used to get the object being visualized, in this case the image.

3. Create a form that contains the controls and logic used to display the object. In the Show() method create an instance of the form, load the object into the form so it's displayed, and use the IDialogVisualizerService.ShowDIalog() method to display the form.

class ImageVisualizer : DialogDebuggerVisualizer 
{
protected override void Show(IDialogVisualizerService windowService, 
IVisualizerObjectProvider objectProvider)
{
//get the image from the object provider
System.Drawing.Bitmap bitmap = 
objectProvider.GetObject() as System.Drawing.Bitmap;

ImageVisualizerUI imageView = new ImageVisualizerUI();
imageView.imageToDisplay.Image = bitmap;
windowService.ShowDialog(imageView);
}
}

4. Add the System.Diagnostics.DebuggerVisualizerAttribute to the AssemblyInfo file, this attributes maps the visualizer to the type it visualizes.

using System.Diagnostics;

[assembly: DebuggerVisualizer(typeof (Namespace.ImageVisualizer), 
Target= typeof(System.Drawing.Bitmap), Description="View image")] 

5. Compile the class (give it a strong name) and copy it to the 'C:\Program Files\Microsoft Visual Studio 8\Common7\Packages\Debugger\Visualizers' directory, or the relevant path depending on where you have installed Visual Studio.

And thats it.... Visual Studio's debugger looks in here whenever you debug and will load the type visualizer as needed. In the example above if your debugging a Bitmap variable you'll get the magnifying glass, click it, hey presto there's your image. So simple, very powerful.

The leading UI suite for ASP.NET - Telerik radControls
Outstanding performance. Full ASP.NET AJAX support. Nearly codeless development.