Using VS - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Using VS arrow Page 2 - Write Your Own Visualizer for VS Debugging
Write Your Own Visualizer for VS Debugging
By John Mueller

Rate This Article: Add This Article To:

Write Your Own Visualizer for VS Debugging - ' Creating a Visualizer '
( Page 2 of 4 )

Visual Studio 2005 developers have access to a new feature that promises to make debugging significantly easier: visualizing data in any form you desire.

With this new version, you can build a debugging visualizer to show you the class in the most realistic ways. Your own classes no longer need to languish without proper debugging support. In short, because you can now see the data as you expect the application or the user to work with it, you save a significant amount of debugging time, and you reduce the potential for errors.

ADVERTISEMENT

Unlike previous Microsoft attempts at making Visual Studio easy to extend, creating a visualizer really is easy. You don't have to engage in odd machinations, or use divination to probe the innermost secrets of Visual Studio. In fact, all you need to do is create a class, add a window, include some simple glue code, and place the resulting DLL in the right location on your hard drive. In this article, I'll show you just how easily you can create a visualizer for the DateTime class.

Creating the Visualizer

A visualizer is a special kind of class that you install within a designated location on your computer. Visual Studio looks at this location for DLLs that contain the correct class information when it initializes. It loads all of the DLLs that match the required criteria, and calls on the functionality the DLL provides when you debug the application. Select a string variable in the Watch window of the debugger, and you'll see options for displaying the string as text, HTML, or XML using the standard visualizers.

Of course, the trick is to create a class DLL that Visual Studio recognizes as a visualizer. Visual Studio 2005 makes this task relatively easy by providing the Microsoft.VisualStudio.DebuggerVisualizers.dll.

Note: I'm using the most current Visual Studio 2005 beta as I write this article. I originally tried creating this project using Visual Basic, but the beta makes it practically impossible to write a visualizer. The release version of Visual Studio isn't supposed to have this limitation, so for the sake of this article, I use C# to create the visualizer.

Begin by creating a new class project. Rename the Class1.cs file to something appropriate; I used DateTimeVisualizer.cs for the project since I'm creating a visualizer for the DateTime object. The IDE should automatically rename the class to DateTimeVisualizer as well.

You need to add a reference to the Microsoft.VisualStudio.DebuggerVisualizers namespace. Right click on the References folder entry in Solution Explorer, and choose Add Reference from the context menu. You'll see the Add Reference dialog box. Choose the Microsoft.VisualStudio.DebuggerVisualizers entry and click OK. Now, add the following line of code to the beginning of your class file:

using Microsoft.VisualStudio.DebuggerVisualizers;

You also want to derive this class from the abstract DialogDebuggerVisualizer class. Type a colon after the class entry and type DialogDebuggerVisualizer. At this point, you can hover the mouse over the DialogDebuggerVisualizer entry and see an implementation option in the SmartTag, as shown in Figure 1. Select this option and the IDE will automatically create all of the required method stubs for your class. At this point, your class is ready to start coding.

Figure 1: Use the SmartTag to view the implement option.

Defining the Viewing Window

A Visualizer, by definition, provides some kind of window for seeing the data in a new way — one that the debugger doesn't currently provide. Add a new window by right clicking the project entry in Solution Explorer and choosing Add | New Item from the context menu. Choose the Windows Form template and call it DateTimeWindow.cs. Click Add. You can add controls to the form, as shown in Figure 2.

Theoretically, you could display another dialog asking the user to select an output format for the universal time. The complexity of the window code is up to you. Simply decide how much information you want to display and in what form you want to display it.



 
 
>>> More Using VS Articles          >>> More By John Mueller
 



HD VOIP Has Arrived (with Tony Konstner)

Play Video >

All Videos >

Google and blonde jokes?

Read now >

Favorite books!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.