Visual C++ 2005 IDE Enhancements, Part 5: Variable Display, Page 2
At some stage, despite all the attempts at clever display techniques, textboxes just don't cut it for displaying large and complex variables. Most developers have had to add a DataGrid to a dummy form so they can debug the contents of a DataSet or copy the data from an XML string into a file so they can view it with the auto-indenting and node-collapsing abilities of Internet Explorer. As variables get more complex, the need for customized displays such as a DataGrid or a specialized XML viewer increases. In recognition of this need, Visual Studio ships with a new technology called Debug Visualizers. A Debug Visualizer is simply a custom display form that is associated with a particular type and accessible from any of the standard debug windows like Autos and Locals.
Before going into the process of writing a Visualizer, take a look at one in action. Figure 5 shows the Autos window with a System::String variable included. Notice the small magnifying glass icon at the end of the variable's value.
Click here for a larger image.
Figure 5. Type with a Visualizer Available
Clicking on the magnifying glass brings up the Visualizer associated with the type (as shown in Figure 6). In this case, the display is of a simple string that was not difficult to inspect with the standard textbox-based debug display, but as the string becomes larger, the usefulness of the string Visualizer increases. Planned for inclusion in the final release (but not working in Beta 2) are Visualizers for DataSets, XML, and HTML.
Figure 6. String Debug Visualizer Display
Writing a Visualizer is a reasonably simple task. Rather than forcing the developer to tread a painful line between the native core of the IDE and the managed code responsible for large sections of the UI, writing Visualizers is purely a managed code exercise. Visual Studio ships with a walkthrough that goes through each step in detail, but the basic tasks involve implementing the IVisualizerObjectProvider interface for the type, which will live inside Visual Studio and display the Visualizer UI, and deriving from VisualizerObjectSource for the type that will live inside the process that is being debugged. The Visualizer user interface is implemented through an IDialogVisualizerService-implementing object, which will display Windows Forms controls. To hook all the types together, the DebuggerVisualizerAttribute is used. An optional feature of a debug visualizer is the ability to modify data in the variable being displayed, and it can do this by changing values in the object or by creating a replacement object.
Deployment of a debug visualizer is a simple story, with no Registry settings required. The assembly containing the visualizer is simply placed in a known directory (either [Visual Studio Install Path]\Microsoft Visual Studio 8\Common7\Packages\Debugger\Visualisers or My Documents\Visual Studio\Visualizers), and Visual Studio uses reflection to pick up the assembly and load the correct types.
About the Author
Nick Wienholt is an independent Windows and .NET consultant based in Sydney, Australia. He is the author of Maximizing .NET Performance from Apress, and specializes in system-level software architecture and development with a particular focus on performance, security, interoperability, and debugging. Nick can be reached at NickW@dotnetperformance.com.
Page 2 of 2