PHenry
PHenry

Reputation: 293

Trying to get a DataRow[] Debugger Visualizer to work in Visual Studio 2010

I'm trying to get a DataRow[] DebuggerVisualizer working for VisualStudio 2010 and unfortunately I can't get it work work. I'm able to get the DataRow one working but not the DataRow[], I would love any please?

The meat of the code is here.


    [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 ) );
      }
    }
  }
}

As I hope you can see, I'm trying to set the Target correctly, but it's not being used by VS at runtime/debugging time. Yes, I am copying the DLLs to the correct Visualizers directory. In fact, I'm using a BuildEvent to do that work for me.


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

When I test this, I use this.


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();
    }

The key here is, the VisualizerDevelopmentHost works correctly, and I can only guess cause it's told which VisualizerObjectSource to use. But when I hit the Debugger.Break(); line and try to use it like normal, I can't see the magnifying glass for the defencemen DataRow[].

I believe in the bottom of my heart this can be done. I read on MSDN DataRow couldn't be done, but I got it to work. I really do hope you can help me out there to get this working.


Thank you very much guys for your replies. You confirmed what I was thinking (well, what I was coming to realize after four nights of fighting with it!). Thanks again. I blogged about it and referenced this information. Thank you very much for your time.

Visual Studio Debugger Visualizers (Take Three)

Upvotes: 4

Views: 1207

Answers (2)

Peter Ritchie
Peter Ritchie

Reputation: 35869

For the most part what Spike says is true. You can write a visualizer for anything "except Object or Array": http://msdn.microsoft.com/en-us/library/e2zc529c.aspx

"Array" seems a little ambiguous; but there's lots of people with the same problem...

I haven't been able to find anything specific to this (and haven't tried it) but, what about IEnumerable? Does that work?

There's also an interesting article of getting around limitations on the types of objects a Visualizer can except here: http://joshsmithonwpf.wordpress.com/2008/01/20/the-rock-star-hack-of-2008/

Upvotes: 3

Spike
Spike

Reputation: 2027

Debug Visualizers don't work for arrays.

You can write a custom visualizer for an object of any managed class except for Object or Array. http://msdn.microsoft.com/en-us/library/e2zc529c.aspx

The visualized type has to be attributed [Serializable] or implement ISerializable. Arrays don't implement ISerializable and cannot be attributed. For some reason.

Lists work, though, so I sometimes make a new list<>, just for debugging purposes.

Upvotes: 1

Related Questions