How to write a custom visualizer

Many people have used and liked the new visualizers in Whidbey. They really are a powerful data viewing tool, especially with the functionality of supporting custom visualizers. While the help documentation for writing your own visualizers is getting finalized, I thought I would provide a quick how-to for this.

To create a custom visualizer in C#, create a C# dll with this code

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualStudio.DebuggerVisualizers;
using System.Diagnostics;

[assembly: DebuggerVisualizer(typeof(CS_Visualizer.Class1), typeof(VisualizerObjectSource),
    Description = "Test Visualizer",
    Target = typeof(System.String))]

[assembly: DebuggerVisualizer(typeof(CS_Visualizer.Class1), typeof(VisualizerObjectSource),
    Description = "Test Visualizer",
    Target = typeof(System.Int32))]

namespace CS_Visualizer
{
    public class Class1 : Microsoft.VisualStudio.DebuggerVisualizers.DialogDebuggerVisualizer
    {
        protected override void Show(Microsoft.VisualStudio.DebuggerVisualizers.IDialogVisualizerService windowService, Microsoft.VisualStudio.DebuggerVisualizers.IVisualizerObjectProvider objectProvider)
        {
            if (windowService != null)
            {
                object data = (object)objectProvider.GetObject();

                Form displayForm = new Form();
                displayForm.Text = data.ToString();
                windowService.ShowDialog(displayForm);
            }
        }
    }
}

You will need to add references to System.Windows.Forms & Microsoft.VisualStudio.DebuggerVisualizers.

Try this code for VB

<Assembly: DebuggerVisualizer(GetType(Class1), GetType(Microsoft.VisualStudio.DebuggerVisualizers.VisualizerObjectSource), Description:="My Test Integer Visualizer", Target:=GetType(Integer))>

<Assembly: DebuggerVisualizer(GetType(Class1), GetType(Microsoft.VisualStudio.DebuggerVisualizers.VisualizerObjectSource), Description:="My Test String Visualizer", Target:=GetType(System.String))>

Public Class Class1
    Inherits Microsoft.VisualStudio.DebuggerVisualizers.DialogDebuggerVisualizer

    Protected Overrides Sub Show(ByVal windowService As Microsoft.VisualStudio.DebuggerVisualizers.IDialogVisualizerService, ByVal objectProvider As Microsoft.VisualStudio.DebuggerVisualizers.IVisualizerObjectProvider)
        If windowService IsNot Nothing Then
            Dim data = objectProvider.GetObject
            Dim displayForm As New Windows.Forms.Form
            displayForm.Text = data.ToString
            windowService.ShowDialog(displayForm)
        End If
    End Sub
End Class

The dll built needs to be copied to either your My Documents\Visual Studio 2005\Visualizers or Common7\packages\debugger\visualizers folder. The latter should have write permission only for admins since it should be under the Program Files folder (by default).

After this, when you view an object of the types specified (integer & string here) in the debugger, you will see the visualizer hourglass, and on clicking it, a form will come up showing the value.

You can easily tweak this code sample for other types, note that

  • only one dll can handle multiple data types, we are handling two types here
  • though here we are just drawing a winform, the code to display the visualizer can be of any complexity.
  • your data type needs to be serializable. I used basic data types here, if you want to create visualizers for your custom classes with multiple fields you will of course need to be able to pass it to Show() for displaying.

Happy visualizing!