Write Your Own Visualizer for VS Debugging - ' Testing the Visualizer ' (
Page 4 of 4 )
Testing the Visualizer
Testing a visualizer could be a time-consuming task, if you had to perform it outside the current project. Fortunately, Microsoft has made a testing technique available that reduces the complexity of the testing process considerably.
ADVERTISEMENT
To use this technique, you add an executable project, such as a console application, to the current solution. In fact, the example does use a console application, but you can use any executable application desired. Right click the new project and select "Set as Startup Project" from the context menu, to ensure the test application runs when you click "Start Debugging." Don't make the test application too complicated; remember that the goal is to test the visualizer.
Once you create the application, add references to both your visualizer class and the Microsoft.VisualStudio.DebuggerVisualizers DLL. You need both references to obtain the resources required for testing. Finally, you can add the test code to the test application. Listing 2 shows the code that the example uses for testing.
Listing 2: Creating a Test Application for the Visualizer
static void Main(string[] args)
{
DateTime ThisTime = DateTime.Now;
VisualizerDevelopmentHost host =
new VisualizerDevelopmentHost(
ThisTime,
typeof(DateTimeVisualizer.DateTimeVisualizer));
host.ShowVisualizer();
Console.WriteLine("The current date and time are:");
Console.WriteLine(ThisTime.ToLongDateString());
Console.WriteLine(ThisTime.ToLongTimeString());
Console.WriteLine("Press Any Key When Ready...");
Console.ReadLine();
}
The Visual Studio IDE doesn't know about the visualizer at this point, but you can simulate this recognition using the VisualizerDevelopmentHost object. The constructor for this object accepts the name of the object you want to test, as well as the type of the class that performs the visualization. Calling the ShowVisualizer() method simulates the act of selecting the visualizer in one of the debugging windows. The example test code includes a sanity check that verifies the output shown in the visualizer window. In addition, this extra code ensures that you can verify the visualizer stops application execution as intended.
Using the Visualizer with a Standard Application
Before you can use a visualizer with your next application, you need to put it in the right location on your hard drive. You must choose between a global or personal visualizer. The location for global visualizers that anyone can use is the VS Install Dir>\Common7\Packages\Debugger\Visualizers folder. Place your own personal visualizers in the My Documents\Visual Studio\Visualizers folder.
After you move the visualizer to its final location, close and reopen Visual Studio. This step ensures the IDE recognizes the new visualizer. Create a new application to test the visualizer. The example uses a simple Windows form application with a single button on it marked Test. Here's the test code.
You can set a break point on the second line. Run the application and look into the Autos window. You should see a magnifying glass icon for MyTime, as shown in Figure 3. Select this icon, and you'll be able to open the new visualizer. The display will look similar to Figure 3.