Creating Debugger Visualizers with Visual Studio 2005

Creating Debugger Visualizers with Visual Studio 2005

ne of the many cool new features in Visual Studio 2005 is the debugger visualizer. Debug info is no longer limited to what comes in the box with Visual Studio .NET. Visual Studio 2005’s new debugger visualizers enable you to inspect classes in more useful and complex ways. You can write your own visualizer for .NET classes or custom classes to see the information that you find useful while debugging. In addition, Microsoft has written a handful of helpful visualizers to get you started.

Visual Studio 2005 has some pre-existing visualizers that you will discover during debug mode. You can access the visualizers by using a dropdown list on the left side of the new DataTips between the selection and its value. (Note that prior to the November Community Technical Preview (aka CTP), the dropdown was on the right. You will find the new position much more convenient.) For example, if you point to a string object in Debug mode as in Figure 1, or even an actual string as in Figure 2, you will have access to three debugger visualizers: Text, XML, or HTML.

You can also access visualizers through the Watch windows. Figure 3 shows the DataTip’s Watch window where you can see the _stringvalue property of a StringBuilder object. When you select the XML visualizer, you will get a better view of the XML-formatted string, demonstrated in Figure 4.


Figure 1. Access Visualizers from Datatips: You can access debugger visualizers from a drop down in the datatips. Here is a debugger visualizer for an object.
?
Figure 2. These visualizers are also available for raw string data in your code.

Another handy visualizer that Microsoft has already written and included with the Beta1 version of Visual Studio 2005 is for DataTables and for DataSets. Something that surely many developers have considered?this visualizer presents a quick way to see the contents of a DataTable. Though they are named “DataSet visualizer” and “DataTable visualizer,” they present the same UI. These two visualizers are read-only. The screenshots in a DataSet visualizer in Figure 5 and Figure 6 show how you can select the various DataTables within a DataSet to view.


Figure 3. Debugger visualizers are also available in the watch windows as seen with this m_stringValue property of a StringBuilder object.
?
Figure 4. An XML visualizer displays XML in a format that is perfectly readable.

Look for more “out-of-the-box” visualizers in future releases of Visual Studio 2005.


Figure 5. Microsoft has also provided pre-written visualizers for DataSets and DataTables.
?
Figure 6. The DataSet and DataTable visualizer shows a grid view of the actual data. You can access all DataTables in a DataSet through the DataSet visualizer’s interface.

Roll Your Own Visualizers
The real beauty of debugger visualizers is that you can write your own. You can create debugger visualizers to display information about .NET Framework classes or your own custom classes.

Because of the possibility that you might accidentally deploy code with your debugging tool built into it, you may consider creating separate tools for invoking and debugging your visualizer debuggers.

Debugger visualizers inherit from the DialogDebuggerVisualizer class which is part of the Microsoft.VisualStudio.DebuggerVisualizers assembly. Note that Microsoft did not include this assembly with the current Express versions of Visual Studio 2005 Beta 1. Since you will need to compile and deploy your visualizers, they are not written in the same project or the same solution as the projects that will actually use them. After you write the required code to identify the visualizer and what class it will work with, you can write whatever code you wish to create the display information.

As an example, Figure 7 and Figure 8 demonstrate two custom visualizers that show some helpful metadata about a DataTable object. The visualizer in Figure 7 builds a string and displays it in a MessageBox while Figure 8 shows the result of a visualizer sending information to various controls in a Windows Form.

Building a Debugger Visualizer
A few ground rules:

  • Each debugger visualizer is a separate class.
  • You can combine multiple debugger visualizers for the same class into one project/assembly.
  • Debugger visualizers for different classes must reside in a separate project.
  • You may have multiple assemblies targeted to the same class.
?
Figure 7: This custom visualizer displays useful metadata about a DataTable inside a MessageBox.

Start by creating a new Class Library project and then add a project reference to the Microsoft.VisualStudio.DebuggerVisualizers dll. By default you’ll find this file at C:Program FilesMicrosoft Visual Studio 8Common7IDEPublicAssembliesMicrosoft.VisualStudio.DebuggerVisualizers.dll.

Create a new class in your project.

Your class must inherit from the DialogDebuggerVisualizer class and also must override the Show method from that class. Here’s the C# version of the code.

   // C#    namespace CustomDV   {   public class DTMsgBox : DialogDebuggerVisualizer       {   public DTMsgBox()       {       }   override protected void Show         (IDialogVisualizerService windowService,         IVisualizerObjectProvider objectProvider)      {   //logic and display code here       }       }   }

And here’s the VB.NET version.

?
Figure 8: This visualizer takes the same metadata from Figure 7, but displays it in a Windows Form.
   ' VB:   Namespace CustomDV   Public Class DTWinForm   Inherits DialogDebuggerVisualizer   Protected Overrides Sub Show( _   ByVal windowService _   As IDialogVisualizerService, _   ByVal objectProvider _   As IVisualizerObjectProvider)   'logic and display code here    End Sub   End Class   End Namespace

Additionally, you will need to create a DebuggerVisualizer attribute for the class. To add the attribute, follow these steps.

  1. Insert a type reference to your class, including a namespace if you have one.
  2. Set the Target to the type of class for which the debugger visualizer will be used to display information.
  3. Set the Description to the actual name that will display in the list of visualizers displayed for that object.

Note that the C# syntax is quite different than Visual Basic.

   // C#:   [assembly:     DebuggerVisualizer(typeof(CustomDV.DTMsgBox),     Target = typeof(System.Data.DataTable),     Description = "MetaData:MessageBox")]   namespace CustomDV   {   public class DTWinForm: DialogDebuggerVisualizer            ' VB:       Namespace CustomDV   Public Class DTWinForm   Inherits DialogDebuggerVisualizer

The objectProvider parameter of the Show method is the key to accessing the class that will be debugged?in this case, a DataTable. So the first step is to cast the objectProvider to a DataTable.

   // C#:   DataTable dt=new DataTable();   dt=(DataTable) objectProvider.GetObject();      ' VB:   Dim dt As DataTable = _   CType(objectProvider.GetObject, DataTable)
?
Figure 9: This screenshot shows all four of our new custom visualizers available from a DataTable object in addition to the default DataTable Visualizer.

Now that the basic part of the visualizer is set up, you can write code to define what to display. For this visualizer, a StringBuilder object was created with metadata from the DataTable.

Listing 1 shows the complete listing for this debugger visualizer in C#. Listing 2 shows the same class written in Visual Basic.

You are not limited to MessageBox displays for your debugger visualizers. Listing 3 gives a C# example of a class for a debugger visualizer that displays a Windows Form. Additionally, the class has a new name that is also reflected in the assembly attribute. The description in the attribute has been changed as well to differentiate it from the MessageBox visualizer. Listing 4 shows this class written in Visual Basic.

Figure 9 demonstrates these four custom visualizers now available during debug mode.

Debugging the Debugger
You can debug your custom debugger visualizer from within another project. The VisualizerDevelopmentHost class in the Microsoft.VisualStudio.DebuggerVisualizers namespace allows you to invoke the debugger visualizer programmatically without having to go through the DataTip to access it. This means you have a hook into your visualizer class and can debug into the project.

Note that an invoked debugger visualizer will run in the compiled application, just like any other class, regardless of whether it is compiled in Debug or Release mode. Therefore it is important that you consider the potential problem of accidentally deploying code with an active visualizer built in, because your end users will, indeed, see the visualizer. It may be more prudent to build a separate testing UI to debug your visualizers.

Here are the steps to debug a debugger visualizer.

  • Load the project for your custom visualizer into the solution for your Windows Form application.
  • Add a reference to the debugger visualizer project to the Windows Form project.
  • Add a reference to Microsoft.VisualStudio.DebuggerVisualizers in the Windows Form project.

At the point in your code that the DataTable has been populated, add the following code to invoke the Debugger Visualizer class passing in the DataTable object and a type reference to visualizer.

   // C#:   DataTable dt =GetADataTable();   VisualizerDevelopmentHost dv =   new VisualizerDevelopmentHost(     dt, typeof(CustomDV.DTMsgBox));   dv.ShowVisualizer();            ' VB:   Dim dt as DataTable=GetADataTable()   Dim dv As VisualizerDevelopmentHost = New _      VisualizerDevelopmentHost(dt, _      GetType(CustomDV.DTMsgBox))   dv.ShowVisualizer()

Because of the possibility that you might accidentally deploy code with your debugging tool built into it, you may consider creating separate tools for invoking and debugging your visualizer debuggers.

Deploying a Custom Debugger Visualizer
Debugger visualizers get stored in a specific place on the developer’s computer. Build your project in Release mode. Then in Windows Explorer, locate the DLL that you have created and copy it to My DocumentsVisual StudioVisualizers. Note that if you have not yet used the out-of-the-box debugger visualizers, you will have to create the Visualizers folder. Also, this location has changed over a number of releases of the Visual Studio 2005 CTPs and Beta, so watch for the chance of it moving again in future releases.

Once the assembly exists in this folder, your custom debugger visualizer becomes part of your Visual Studio 2005 debugging environment. Naturally, you can share your custom debugger visualizers with other developers by distributing the assemblies.

With all that is new and exciting about Visual Studio 2005 and .NET Framework 2.0, this fantastic new feature, one of a number of great additions to Diagnostics in the .NET Framework, has been sorely overlooked so far. As more and more developers realize the beauty of this class, watch the community space for custom visualizers that other developers have written.

devx-admin

devx-admin

Share the Post:
Economy Act Soars

Virginia’s Clean Economy Act Soars Ahead

Virginia has made significant strides towards achieving its short-term carbon-free objectives as outlined in the Clean Economy Act of 2020. Currently, about 44,000 megawatts (MW)

Renewable Storage Innovation

Innovative Energy Storage Solutions

The Department of Energy recently revealed a significant investment of $325 million in advanced battery technologies to store excess renewable energy produced by solar and

Development Project

Thrilling East Windsor Mixed-Use Development

Real estate developer James Cormier, in collaboration with a partnership, has purchased 137 acres of land in Connecticut for $1.15 million with the intention of

USA Companies

Top Software Development Companies in USA

Navigating the tech landscape to find the right partner is crucial yet challenging. This article offers a comparative glimpse into the top software development companies

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in

Economy Act Soars

Virginia’s Clean Economy Act Soars Ahead

Virginia has made significant strides towards achieving its short-term carbon-free objectives as outlined in the Clean Economy Act of 2020. Currently, about 44,000 megawatts (MW) of wind, solar, and energy

Renewable Storage Innovation

Innovative Energy Storage Solutions

The Department of Energy recently revealed a significant investment of $325 million in advanced battery technologies to store excess renewable energy produced by solar and wind sources. This funding will

Renesas Tech Revolution

Revolutionizing India’s Tech Sector with Renesas

Tushar Sharma, a semiconductor engineer at Renesas Electronics, met with Indian Prime Minister Narendra Modi to discuss the company’s support for India’s “Make in India” initiative. This initiative focuses on

Development Project

Thrilling East Windsor Mixed-Use Development

Real estate developer James Cormier, in collaboration with a partnership, has purchased 137 acres of land in Connecticut for $1.15 million with the intention of constructing residential and commercial buildings.

USA Companies

Top Software Development Companies in USA

Navigating the tech landscape to find the right partner is crucial yet challenging. This article offers a comparative glimpse into the top software development companies in the USA. Through a

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in and explore the leaders in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, and kickstart your journey to

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner for your online project. Your

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not

Cybersecurity Strategy

Five Powerful Strategies to Bolster Your Cybersecurity

In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies

Global Layoffs

Tech Layoffs Are Getting Worse Globally

Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data

Huawei Electric Dazzle

Huawei Dazzles with Electric Vehicles and Wireless Earbuds

During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting

Cybersecurity Banking Revolution

Digital Banking Needs Cybersecurity

The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising

FinTech Leadership

Terry Clune’s Fintech Empire

Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned

The Role Of AI Within A Web Design Agency?

In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing

Generative AI Revolution

Is Generative AI the Next Internet?

The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These

Microsoft Laptop

The New Surface Laptop Studio 2 Is Nuts

The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over

5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These

Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created

Cisco Splunk Deal

Cisco Splunk Deal Sparks Tech Acquisition Frenzy

Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the

Iran Drone Expansion

Iran’s Jet-Propelled Drone Reshapes Power Balance

Iran has recently unveiled a jet-propelled variant of its Shahed series drone, marking a significant advancement in the nation’s drone technology. The new drone is poised to reshape the regional