Share via


Creating a debugger visualizer for Visual Studio (C# tutorial)

This article was originally published here.

What is a “debugger visualizer”?

Here’s a direct quote from MSDN:

A visualizer creates a dialog box or other interface to displays a variable or object in a meaningful way that is appropriate to its data type. For example, an HTML visualizer interprets an HTML string and displays the result as it would appear in a browser window, a bitmap visualizer interprets a bitmap structure and displays the graphic it represents, and so on. Some visualizers allow you to edit as well as view the data.

Considering the context – custom debugger tools – creating a custom debugger visualizer is surprisingly easy. I would expect this to involve native code, COM objects, cumbersome registry editing and a week at a forced labor camp, or any combination of the above. Turns out it’s a walk in the park.

Wanna get started?

For those of you eager to begin, here is a summary of the steps needed. I’ll explain everything in detail after the jump.

  1. Create a class library project.
  2. Add reference to Microsoft.VisualStudio.DebuggerVisualizers (.NET reference)
  3. Add reference to System.Windows.Forms (.NET reference)
  4. Create a class that inherits from DialogDebuggerVisualizer
  5. Implement the single method required (very easy implementation).
  6. Add the DebuggerVisualizer assembly attribute to indicate which type are you handling and what is the class handling that type.
  7. Build the DLL and copy it to <My Documents> \Visual Studio 2005\Visualizers
  8. When you’re debugging – hover over a variable of the type you’re handling and click the magnifying glass icon.

That’s all you need to do.

Details explained

Steps 1-4 in the above list are pretty straight forward, so let’s jump right into step 5.

I’ve decided to create my visualizer for the type Color. The visualizer will display a small form filled with the color specified in the variable inspected by the debugger.

    1:  using System.Diagnostics;
    2:  using System.Drawing; // add reference to System.Drawing .net lib for this line to compile.
    3:  using System.Windows.Forms;
    4:  using Microsoft.VisualStudio.DebuggerVisualizers;
    5:  [assembly : DebuggerVisualizer(
    6:    typeof (ColorVisualizer),
    7:    Target = typeof (Color),
    8:    Description = "Color Visualizer")]
    9:   
   10:  public class ColorVisualizer : DialogDebuggerVisualizer {
   11:    protected override void Show(IDialogVisualizerService svc, IVisualizerObjectProvider provider) {
   12:      Color c = (Color) provider.GetObject();
   13:      using (Form form = new Form()) {
   14:        form.Text = "Color Visualizer - by Lior Elia";
   15:        form.BackColor = c;
   16:        form.Size = new Size(400, 400);
   17:        svc.ShowDialog(form);
   18:      }
   19:    }
   20:  }
   21:   

Asyou can see, the code is really straight forward. Get the object, create a form (can be a pre-prepared form if it's a complex visualizer) and display the data. That's it!

Another interesting thing to notice is regarding steps 7-8. You don't need to restart visual studio in order to use your new visualizer. This means that you can make changes and fixes to your code and just drop the new DLL in the visualizers folder in order for Visual Studio to use it. It's that easy!

Important note: you need a reference to System.Drawing for the Color class to be accessible in your code.

Comments