试图让DataRow [] Debugger Visualizer在Visual Studio 2010中工作

时间:2010-09-28 03:25:56

标签: visual-studio visual-studio-2010

我正在尝试使用DataRow [] DebuggerVisualizer为VisualStudio 2010工作,不幸的是我无法让它工作。我能够让DataRow工作但不是DataRow [],我会喜欢吗?

代码的内容就在这里。


    [assembly: DebuggerVisualizer(
        typeof( PCHenry.DR ),
        typeof( PCHenry.DRObjectSource ),
        Target = typeof( DataRow[] ),
        Description = "DataRow Array Debugger Visualizer (or so if you see this then it's working YAHOO!)" )]
    namespace PCHenry
    {
      public class DR : DialogDebuggerVisualizer
      {
        protected override void Show( IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider )
        {
          StringBuilder stringToDebug = new StringBuilder();
          using( Stream dataStream = objectProvider.GetData() )
          {
            BinaryFormatter formatter = new BinaryFormatter();
            string incomingData = formatter.Deserialize( dataStream ) as string;
            stringToDebug.Append( string.Format( "*!!!!{0}!!!!*", incomingData ) );
          }

          MessageBox.Show( stringToDebug.ToString(), "PCH String Debugger Visualizer", MessageBoxButtons.OK, MessageBoxIcon.Asterisk );
        }
      }

  public class DRObjectSource : VisualizerObjectSource
  {
    public override void GetData( object target, Stream outgoingData )
    {
      if( target != null && target is DataRow[] )
      {
        DataRow[] rows = target as DataRow[];
        BinaryFormatter formatter = new BinaryFormatter();
        //formatter.Serialize( outgoingData, target );

        formatter.Serialize( outgoingData, string.Format( "There are {0} rows of data", rows.Length ) );
      }
    }
  }
}

正如我希望你能看到的那样,我正在尝试正确设置Target,但是在运行时/调试时它没有被VS使用。是的,我正在将DLL复制到正确的Visualizers目录。事实上,我正在使用BuildEvent为我做这项工作。


xcopy "$(SolutionDir)$(ProjectName)\$(OutDir)$(TargetFileName)" "$(USERPROFILE)\Documents\Visual Studio 2010\Visualizers" /y

当我测试时,我使用它。


static void Main( string[] args )
    {
      //String myName = "Peter Henry";
      #region DataSetup, create a Habs DataTable and populate it with players
      DataTable table = new DataTable( "Habs" );
      table.Columns.Add( "PlayerNumber", typeof( Int32 ) );
      table.Columns.Add( "PlayerName", typeof( string ) );
      table.Columns.Add( "Position", typeof( string ) );

      //team as current as 09-23-2010 from the Canadiens!  GO HABS GO!
      table.Rows.Add( new object[] { 32, "Travis Moen", "F" } );
      table.Rows.Add( new object[] { 94, "Tom Pyatt", "F" } );
      table.Rows.Add( new object[] { 75, "Hal Gill", "D" } );
      table.Rows.Add( new object[] { 26, "Josh Gorges", "D" } );
      table.Rows.Add( new object[] { 76, "P.K. Subban", "D" } );
      table.Rows.Add( new object[] { 35, "Alex Auld", "G" } );
      #endregion

      //use this to show the debugger in two different ways
      DataRow[] defencemen = table.Select( "Position = 'D'", "PlayerNumber" );

      //this proves this works when told which ObjectSource to use
      VisualizerDevelopmentHost host = new VisualizerDevelopmentHost( 
          defencemen, typeof( PCHenry.DR ), 
          typeof( PCHenry.DRObjectSource ) );
      host.ShowVisualizer();

      //but when I try to use VS debugging here, it can't seem to find the custom DebuggerVisualizer as I would expect
      defencemen = table.Select( "Position = 'D'", "PlayerNumber" );
      Debugger.Break();

      Console.WriteLine( "FIN" );
      Console.ReadLine();
    }

这里的关键是,VisualizerDevelopmentHost工作正常,我只能猜测是因为它告诉了VisualizerObjectSource使用哪个。但是当我点击Debugger.Break();线和尝试使用它像平常一样,我看不到放大镜为防守DataRow []。

我相信可以做到这一点。我在MSDN上阅读DataRow无法完成,但我得到了它的工作。我真的希望你能帮助我实现这个目标。


非常感谢你们的回复。你确认了我的想法(好吧,经过四个晚上的战斗后我才意识到这一点!)。再次感谢。我在博客上写了这篇文章,并引用了这些信息非常感谢你的时间。

Visual Studio Debugger Visualizers (Take Three)

2 个答案:

答案 0 :(得分:3)

Spike所说的大多数情况都属实。您可以为“除了对象或数组”以外的任何内容编写可视化工具:http://msdn.microsoft.com/en-us/library/e2zc529c.aspx

“数组”似乎有点含糊不清;但是有很多人有同样的问题...

我无法找到任何特定的内容(并没有尝试过)但是,IEnumerable怎么样?这有用吗?

还有一篇有趣的文章解决了Visualizer可以对象的类型限制,除了这里:http://joshsmithonwpf.wordpress.com/2008/01/20/the-rock-star-hack-of-2008/

答案 1 :(得分:1)

Debug Visualizers不适用于数组。

  

您可以为其编写自定义可视化工具   除了之外的任何托管类的对象   对象或数组。   http://msdn.microsoft.com/en-us/library/e2zc529c.aspx

可视化类型必须归因于[Serializable]或实现ISerializable。 数组不实现ISerializable,不能归因。出于某种原因。

列表工作,但我有时会创建一个新列表<>,仅用于调试目的。

相关问题