| Programming | Software Engineering | Web Design | Database | Operating Systems

How to write a custom visualizer

deeptanshuv/
Keywords: C#,.net,dll,custom visualizer
From: http://blogs.msdn.com/deeptanshuv/archive/2005/03/21/400194.aspx

Many people have used and liked the new visualizers in Whidbey. They really are a powerful data viewing tool, especially with the functionality of supporting custom visualizers. While the help documentation for writing your own visualizers is getting finalized, I thought I would provide a quick how-to for this.

To create a custom visualizer in C#, create a C# dll with this code

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualStudio.DebuggerVisualizers;
using System.Diagnostics;

[assembly: DebuggerVisualizer(typeof(CS_Visualizer.Class1), typeof(VisualizerObjectSource),
    Description = "Test Visualizer",
    Target = typeof(System.String))]

[assembly: DebuggerVisualizer(typeof(CS_Visualizer.Class1), typeof(VisualizerObjectSource),
    Description = "Test Visualizer",
    Target = typeof(System.Int32))]

namespace CS_Visualizer
{
    public class Class1 : Microsoft.VisualStudio.DebuggerVisualizers.DialogDebuggerVisualizer
    {
        protected override void Show(Microsoft.VisualStudio.DebuggerVisualizers.IDialogVisualizerService windowService, Microsoft.VisualStudio.DebuggerVisualizers.IVisualizerObjectProvider objectProvider)
        {
            if (windowService != null)
            {
                object data = (object)objectProvider.GetObject();

                Form displayForm = new Form();
                displayForm.Text = data.ToString();
                windowService.ShowDialog(displayForm);
            }
        }
    }
}

You will need to add references to System.Windows.Forms & Microsoft.VisualStudio.DebuggerVisualizers.

Try this code for VB

<Assembly: DebuggerVisualizer(GetType(Class1), GetType(Microsoft.VisualStudio.DebuggerVisualizers.VisualizerObjectSource), Description:="My Test Integer Visualizer", Target:=GetType(Integer))>

<Assembly: DebuggerVisualizer(GetType(Class1), GetType(Microsoft.VisualStudio.DebuggerVisualizers.VisualizerObjectSource), Description:="My Test String Visualizer", Target:=GetType(System.String))>

Public Class Class1
    Inherits Microsoft.VisualStudio.DebuggerVisualizers.DialogDebuggerVisualizer

    Protected Overrides Sub Show(ByVal windowService As Microsoft.VisualStudio.DebuggerVisualizers.IDialogVisualizerService, ByVal objectProvider As Microsoft.VisualStudio.DebuggerVisualizers.IVisualizerObjectProvider)
        If windowService IsNot Nothing Then
            Dim data = objectProvider.GetObject
            Dim displayForm As New Windows.Forms.Form
            displayForm.Text = data.ToString
            windowService.ShowDialog(displayForm)
        End If
    End Sub
End Class

The dll built needs to be copied to either your My Documents\Visual Studio 2005\Visualizers or Common7\packages\debugger\visualizers folder. The latter should have write permission only for admins since it should be under the Program Files folder (by default).

After this, when you view an object of the types specified (integer & string here) in the debugger, you will see the visualizer hourglass, and on clicking it, a form will come up showing the value.

You can easily tweak this code sample for other types, note that

Happy visualizing!


Related Article
  • What’s the deal with the C# “using” construct
  • C# Team Is Hiring
  • Table of Contents: GridView Examples for ASP.NET 2.0
  • GridView Examples for ASP.NET 2.0: Improvements in Data Access and Display
  • Create a Search Application with Word 2003 and Visual Basic .NET

  • Comment
    1001 Post At: 2007-8-28 5:46:16
    abc
    163 Post At: 2007-9-21 10:23:11
    1

    zzy Post At: 2008-3-20 23:24:19
    abc.
    Add Your Comment:
    Your Name:      
    Your Comment:
    Note: After you post comment,please refresh the browser to show you comment.
    Search In YeYan.CN:
     

    Home | Privacy Policy | Copyright Policy | Contact Us | Site Map
    Copyright © 2006 YeYan.CN, All Rights Reserved.