The Visual Studio 2005 Debugger includes a great new feature in which a running instance of an object can be inspected in a very intuitive and visual way using what are called Data Visualizers. VS 2005 comes with many visualizers included, however as this featured blog posts shows, you can also create custom visualizers for your classes.

With examples in both C# and VB.NET, this blog posts shows how simple it is to create a custom visualizer that can be extended to show the contents of your custom objects. It also includes instructions on deploying the visualizer assemblies so that Visual Studio can find them.

在VS.NET2005的调试器中包含一个非常好用的新特性-数据观察器(Data Visualizers),它能使我们对程序运行中的一个对象的实例通过可视化的方法进行检查。VS2005有很多观察器的,这里我们自己建立一个为自己的类写的自定义DV。

例子使用了C#和VB.NET代码的,这里只列出C#的。一下是Blog的正文,意思还是比较好理解的。

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.

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

  • only one dll can handle multiple data types, we are handling two types here
  • though here we are just drawing a winform, the code to display the visualizer can be of any complexity.
  • your data type needs to be serializable. I used basic data types here, if you want to create visualizers for your custom classes with multiple fields you will of course need to be able to pass it to Show() for displaying.

Happy visualizing!

原文BLOG地址:http://blogs.msdn.com/deeptanshuv/archive/2005/03/21/400194.aspx

这个简单的Simple能够让您先体验一下自定义数据观察器,下面的这篇文章来自Scott Nonnenberg,是C#组的一个项目经理,隶属于VS Debugger组的,只是偏重于C#方向,下面的这篇文章篇幅长了一点,配有图释,比较容易理解,可以通过连接查看:
Debugger Visualizers on VS 2005 Community Tech Preview
http://blogs.msdn.com/scottno/archive/2004/04/17/115328.aspx

posted on 2005-03-24 09:09  维生素C.NET  阅读(1998)  评论(0编辑  收藏  举报