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 3 - 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 - ' Adding the Visualizer Code '
( Page 3 of 4 )

Adding the Visualizer Code

Now that you have a shiny new visualizer window, it's time to complete the visualizer class. The first thing to remember is that this code always executes halfway between the Visual Studio IDE and the test application, so you can't assume some of the things that you would normally assume about a class.

ADVERTISEMENT

For example, you don't really know how a change will affect the application environment, so you always have to use the built in classes to communicate with the application. Listing 1 shows the class code for this example.

The focus of a visualizer is the Show() method that the IDE automatically adds when it implements the DialogDebuggerVisualizer class. The example begins by making some necessarily checks. For example, you need to know whether the object is null because the visualizer shouldn't process null objects. Always perform conversions within a try...catch block to ensure the application environment doesn't crash, due to a problem with the input from the Visual Studio IDE.

Listing 1: Creating a Visualizer Class

protected override void Show(
   IDialogVisualizerService windowService, 
   IVisualizerObjectProvider objectProvider)
{
   DateTime ThisValue;

   // Verify the object isn't null.
   if (objectProvider.GetObject() == null)
      return;

   // Make sure the object converts to a DateTime.
   try
   {
      ThisValue = Convert.ToDateTime(objectProvider.GetObject());
   }
   catch
   {
      return;
   }

   // Create the visualizer window and pass it the data.
   DateTimeWindow DTW = new DateTimeWindow(ThisValue);
   windowService.ShowDialog(DTW);
}

Now that you have a data value to process and it's in the correct format, you can display the visualizer window created earlier. Creating the window is the same as any other window you've created in the past. However, notice that you don't use the form's ShowDialog() method to display the window. If you use the form's ShowDialog() method, the Visual Studio IDE is unaware of your visualizer status and could continue execution before your visualizer window is closed. Always use the technique shown in the example to display the visualizer window.

Making Visual Studio 2005 Aware of the Visualizer

Your DLL resides in a special folder on your hard drive. However, just placing the file into the correct folder isn't enough to get Visual Studio to recognize it as a visualizer. You also need to add a special attribute to the assembly, as shown here, for the example.

[assembly: System.Diagnostics.DebuggerVisualizer(
   typeof(DateTimeVisualizer.DateTimeVisualizer),
   Target = typeof(System.DateTime),
   Description = "View DateTime Forms")]

The three arguments for this attribute include type of the class that contains the visualizer, the type of the target object, and a description of the visualizer. In this case, we're targeting the DateTime object. However, you can target any kind of object you want.

To target a custom type, you have to provide a reference to its declaration. The description you provide appears in the window when the developer using the visualizer debugs an application. Consequently, it's important that you provide a description that is both clear and related to the object at hand.



 
 
>>> 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.