The blog has moved to a new address. The blog is now located at http://devintelligence.com

Adsense

Tuesday, May 16, 2006

Universal Debugger Visualizer in Visual Studio 2005.

Visual Studio 2005 shipped with a very helpful feature called debugger visualizers.Debugger visualizers allow you to view useful information about objects during debug.This visualizer is helpful for anyone writing an application and want to view or change objects properties during runtime.


using System;

using System.Collections.Generic;

using System.Diagnostics;

using System.Text;

using Microsoft.VisualStudio.DebuggerVisualizers;

using System.Windows.Forms;

using System.Drawing;



namespace Devintelligence.Debug

{


/// <summary>

/// Visual Studio visualizer for viewing objects

/// in PropertyGrid during debug.

/// </summary>

public class UniversalVisualizer : DialogDebuggerVisualizer

{

protected override void Show(IDialogVisualizerService windowService,

IVisualizerObjectProvider objectProvider)

{


object data = objectProvider.GetObject();

using (Form form = new Form())

{

form.Text = data.GetType().FullName;

form.ClientSize = new Size(230, 300);

form.FormBorderStyle = FormBorderStyle.FixedToolWindow;


PropertyGrid propGrid = new PropertyGrid();

propGrid.SelectedObject = data;

propGrid.Parent = form;

propGrid.Dock = DockStyle.Fill;


windowService.ShowDialog(form);

objectProvider.ReplaceObject( propGrid.SelectedObject );

}

}


// TODO: Add the following to your testing code to test the visualizer:

//

// UniversalVisualizer.TestShowVisualizer(new SomeType());

//

/// <summary>

/// Tests the visualizer by hosting it outside of the debugger.

/// </summary>

/// <param name="objectToVisualize">

/// The object to display in the visualizer.</param>

public static void TestShowVisualizer(object objectToVisualize)

{

VisualizerDevelopmentHost visualizerHost =

new VisualizerDevelopmentHost(objectToVisualize,

typeof(UniversalVisualizer));

visualizerHost.ShowVisualizer();

}

}

}


I overrode the method Show. This method gets two parameters: objectProvider holds a serialized copy of the debugged object we want to view or change properties, windowService allows us to show a dialog form under the context of the Visual Studio IDE. In this method, I created a form with a property grid and loaded the object into it.

Here's screenshot of the Universal Visualizer in action.
Universal Visualizer

Technorati : , , , , , ,
Ice Rocket : , , , , , ,

No comments: