Visual Studio 2005 Visualizers

by James Avery





Listing One



using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.VisualStudio.DebuggerVisualizers;

using System.Runtime.Serialization.Formatters.Binary;



namespace Base64StringVisualizer

{

    class Base64VisualizerObjectSource : VisualizerObjectSource

    {

     public override void GetData(object target,System.IO.Stream outgoingData)

     {

        BinaryFormatter formatter = new BinaryFormatter();

        formatter.Serialize(outgoingData, target);

     }

  }

}





Listing Two



using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using Microsoft.VisualStudio.DebuggerVisualizers;

using System.Runtime.Serialization.Formatters.Binary;



namespace Base64StringVisualizer

{

   public partial class Base64StringVisualizerForm : Form

   {

     private IVisualizerObjectProvider _objectSource;

     public Base64StringVisualizerForm(IVisualizerObjectProvider objectSource)

     {

         _objectSource = objectSource;

         InitializeComponent();

     }

     private void Form1_Load(object sender, EventArgs e)

     {

        PopulateForm();

     }

     private void PopulateForm()

     {

        BinaryFormatter formatter = new BinaryFormatter();

        string base64string =

                 (string) formatter.Deserialize(_objectSource.GetData());

        tbBase64.Text = base64string;

            tbPlainText.Text = base64Decode(base64string);

     }

     private string base64Decode(string base64string)

     {

         try

         {

             UnicodeEncoding encoder = new UnicodeEncoding();

             string textString =

                   encoder.GetString(Convert.FromBase64String(base64string));

             return textString;

         }

         catch (Exception e)

         {

             throw new Exception("Error during decoding", e);

         }

     }

     private string base64Encode(string textString)

     {

         try

         {

            string base64string =

                Convert.ToBase64String(ConvertStringToByteArray(textString));

            return base64string;

         }

         catch (Exception e)

         {

             throw new Exception("Error during encoding", e);

         }

    }

    public static byte[] ConvertStringToByteArray(string stringToConvert)

    {

         return (new UnicodeEncoding()).GetBytes(stringToConvert);

    }

    private void btnCancel_Click(object sender, EventArgs e)

    {

        this.Close();

    }

    private void btnOK_Click(object sender, EventArgs e)

    {

         string base64string = base64Encode(tbPlainText.Text);

         _objectSource.ReplaceObject(base64string);

         this.Close();

    }

  }

}





Listing Three



using Microsoft.VisualStudio.DebuggerVisualizers;

using System;

using System.Collections.Generic;

using System.Text;

using System.Diagnostics;



[assembly: DebuggerVisualizer(typeof(Base64StringVisualizer.DebuggerSide),

   typeof(Base64StringVisualizer.Base64VisualizerObjectSource),

        Target = typeof(string), Description = "Base64 String Visualizer")]



namespace Base64StringVisualizer

{

        class DebuggerSide : DialogDebuggerVisualizer

        {

                protected override void Show(IDialogVisualizerService

windowService, IVisualizerObjectProvider objectProvider)

                {

                          Base64StringVisualizerForm form = new

Base64StringVisualizerForm(objectProvider);

                          windowService.ShowDialog(form);

                }

        }

}





Listing Four



string base64string = "This is a test of the Base64 system";

base64string = base64Encode(s); //using method from Listing 2



VisualizerDevelopmentHost visualizerHost =

 new VisualizerDevelopmentHost(base64string, typeof(Base64StringVisualizer));

visualizerHost.ShowVisualizer();

