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();

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
        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!

Comments (7)

  1. Please note this code does not work under beta1. I presume it’s beta2 code because under beta1 after referencing Microsoft.VisualStudio.DebuggerVisualizers;

    there is no such namespace.

  2. Hi Robert,

    Yes, you are right, this is Beta2 code. For Beta1 I think this post should work –



  3. Jessica says:

    I have tried the beta1 code for visualizers, but it seemed like I couldn’t create a visualizer for array types. Is this correct? If so, is support for array types planned for the future?

  4. deeptanshuv says:

    Hi Jessica,

    You are right, custom visualizers cannot handle arrays directly. This is because of design decisions based on factors like the inability of arrays to be attributed.

    You could either abstract the array in a data type and pass that to the visualizer which could reconstruct it, or (much better) you could use an ArrayList. An Arraylist will allow you to easily pass collections of objects to your custom visualizers’s Show() method.



  5. PaulYuk and I just returned from the EMEA tour where we demoed tons of new VB 2005 features to groups…

  6. PaulYuk and I just returned from the EMEA tour where we demoed tons of new VB 2005 features to groups

  7. In Visual Studio 2003 it was sometimes difficult to examine large complicated objects in the debugger