Browse DevX
Sign up for e-mail newsletters from DevX


Creating Debugger Visualizers with Visual Studio 2005 : Page 2

With debugger visualizers, you can define the information you would like to see about a specific .NET or custom class during debug mode. And as the name suggests, you can also choose how you would like to visualize these details.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Roll Your Own Visualizers
The real beauty of debugger visualizers is that you can write your own. You can create debugger visualizers to display information about .NET Framework classes or your own custom classes.

Because of the possibility that you might accidentally deploy code with your debugging tool built into it, you may consider creating separate tools for invoking and debugging your visualizer debuggers.
Debugger visualizers inherit from the DialogDebuggerVisualizer class which is part of the Microsoft.VisualStudio.DebuggerVisualizers assembly. Note that Microsoft did not include this assembly with the current Express versions of Visual Studio 2005 Beta 1. Since you will need to compile and deploy your visualizers, they are not written in the same project or the same solution as the projects that will actually use them. After you write the required code to identify the visualizer and what class it will work with, you can write whatever code you wish to create the display information.

As an example, Figure 7 and Figure 8 demonstrate two custom visualizers that show some helpful metadata about a DataTable object. The visualizer in Figure 7 builds a string and displays it in a MessageBox while Figure 8 shows the result of a visualizer sending information to various controls in a Windows Form.

Building a Debugger Visualizer
A few ground rules:

  • Each debugger visualizer is a separate class.
  • You can combine multiple debugger visualizers for the same class into one project/assembly.
  • Debugger visualizers for different classes must reside in a separate project.
  • You may have multiple assemblies targeted to the same class.
Figure 7: This custom visualizer displays useful metadata about a DataTable inside a MessageBox.
Start by creating a new Class Library project and then add a project reference to the Microsoft.VisualStudio.DebuggerVisualizers dll. By default you'll find this file at C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.DebuggerVisualizers.dll.

Create a new class in your project.

Your class must inherit from the DialogDebuggerVisualizer class and also must override the Show method from that class. Here's the C# version of the code.

// C# namespace CustomDV { public class DTMsgBox : DialogDebuggerVisualizer { public DTMsgBox() { } override protected void Show (IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider) { //logic and display code here } } }

And here's the VB.NET version.

Figure 8: This visualizer takes the same metadata from Figure 7, but displays it in a Windows Form.

' VB: Namespace CustomDV Public Class DTWinForm Inherits DialogDebuggerVisualizer Protected Overrides Sub Show( _ ByVal windowService _ As IDialogVisualizerService, _ ByVal objectProvider _ As IVisualizerObjectProvider) 'logic and display code here End Sub End Class End Namespace

Additionally, you will need to create a DebuggerVisualizer attribute for the class. To add the attribute, follow these steps.

  1. Insert a type reference to your class, including a namespace if you have one.
  2. Set the Target to the type of class for which the debugger visualizer will be used to display information.
  3. Set the Description to the actual name that will display in the list of visualizers displayed for that object.
Note that the C# syntax is quite different than Visual Basic.

// C#: [assembly: DebuggerVisualizer(typeof(CustomDV.DTMsgBox), Target = typeof(System.Data.DataTable), Description = "MetaData:MessageBox")] namespace CustomDV { public class DTWinForm: DialogDebuggerVisualizer ' VB: <Assembly: DebuggerVisualizer(GetType(CustomDV.DTMsgBox), _ Target:=GetType(System.Data.DataTable), _ Description:="MetaData:VB MessageBox")> Namespace CustomDV Public Class DTWinForm Inherits DialogDebuggerVisualizer

The objectProvider parameter of the Show method is the key to accessing the class that will be debugged—in this case, a DataTable. So the first step is to cast the objectProvider to a DataTable.

// C#: DataTable dt=new DataTable(); dt=(DataTable) objectProvider.GetObject(); ' VB: Dim dt As DataTable = _ CType(objectProvider.GetObject, DataTable)

Figure 9: This screenshot shows all four of our new custom visualizers available from a DataTable object in addition to the default DataTable Visualizer.
Now that the basic part of the visualizer is set up, you can write code to define what to display. For this visualizer, a StringBuilder object was created with metadata from the DataTable.

Listing 1 shows the complete listing for this debugger visualizer in C#. Listing 2 shows the same class written in Visual Basic.

You are not limited to MessageBox displays for your debugger visualizers. Listing 3 gives a C# example of a class for a debugger visualizer that displays a Windows Form. Additionally, the class has a new name that is also reflected in the assembly attribute. The description in the attribute has been changed as well to differentiate it from the MessageBox visualizer. Listing 4 shows this class written in Visual Basic.

Figure 9 demonstrates these four custom visualizers now available during debug mode.

Thanks for your registration, follow us on our social networks to keep up-to-date