none
8. How do I implement a custom debug visualizer?

Answers

  • Both Visual Studio 2005 and Visual Studio 2008 have a template called Debugger Visualizer. To create a custom debugger visualizer, you can rewrite the Show method of the template. Below is a walkthrough to create a custom debugger visualizer for a custom type:

    1.         Create a Class Library project and add two classes named, for example, Student and OurClass, separately. They are the target custom type you want to use in our debugger visualize. Following is the code snippet:

    namespace TestVisualizer

    {

        [Serializable]

        public class Student

        {.....}

        [Serializable]

        public class OurClass

        {.....}

     }

    2.         Create another Class Library project, and add a debugger visualizer Item in the project (also, add a reference to the previous project.)

    [assembly: System.Diagnostics.DebuggerVisualizer(typeof(CustomVisualizer.OurClassVisualizer),

     Target = typeof(TestVisualizer.OurClass), Description = "Custom Visualizer")]

    namespace CustomVisualizer

    {

        public class OurClassVisualizer : DialogDebuggerVisualizer

       {

              protected override void Show(IDialogVisualizerService windowService,

             IVisualizerObjectProvider objectProvider)

             {

                   OurClass oc = (OurClass)objectProvider.GetObject();

                   using (VisualizerForm displayForm = new VisualizerForm())

                 {

                     displayForm.list = oc.ClassList;

                     displayForm.average = oc.AverageAge;

                     windowService.ShowDialog(displayForm);

                 }

            }

                 ....

    3.         Implement the VisualizerForm class. Right click the project name and add a windows form class, drag and drop some controls (a DataGridView control and a Label control.) Double click the form, add a code snippet as below:

    public partial class VisualizerForm : Form

    {

            public VisualizerForm()

            {

                InitializeComponent();

            }

            public List<Student> list = null;

            public int average = 0;

            private void VisualizerForm_Load(object sender, EventArgs e)

            {

                this.lblage.Text = average.ToString();

                this.dataGridView1.DataSource = list;

              this.dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);

            }

        }

    4.         Build the project, copy the dll files to the folder:
    <Drive>:\Program Files\Microsoft Visual Studio 9.0\Common7\Packages\Debugger\Visualizers
    and
    <Drive>:\My Documents\Visual Studio 2008\Visualizers

    5.         Create a Windows Forms project to test the Visualizer, after the breakpoint was hit, hovering our cursor on the variable and click the glasses icon. Our Visualizer appears.

    For more information on how to implement a debugger visualizer, please refer to:
    How to: Write a Visualizer:
    http://msdn.microsoft.com/en-us/library/e2zc529c.aspx
    How to write a custom visualizer:
    http://blogs.msdn.com/deeptanshuv/archive/2005/03/21/400194.aspx

     

    • Marked as answer by MSDN FAQ Monday, May 24, 2010 8:54 AM
    Monday, May 24, 2010 8:54 AM