Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Visualizer for IEnumerable<string>

TL;DR
Does anyone know how to write a Debug Visualizer for Visual Studio 2012, in C#, so I can visualize IEnumerable<string>, string[] or similar objects?

More Info
Visual Studio Debug Visualizer are great, and I use some popular ones (Mole) regularly. However, now the time has come to roll out some custom visualizers. I started off with a simple visualizer for a string:

[assembly: System.Diagnostics.DebuggerVisualizer(typeof(My.Namespace.DebuggerSide),
                                            typeof(VisualizerObjectSource),
                                            Target = typeof(string),
                                            Description = "Awesome Visualizer")]

the code of DebuggerSide is basically the example from the template:

public class DebuggerSide : DialogDebuggerVisualizer
{
    protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
    {
        if (windowService == null)
            throw new ArgumentNullException("windowService");
        if (objectProvider == null)
            throw new ArgumentNullException("objectProvider");


        var data = (string)objectProvider.GetObject();

        using (var displayForm = new VisualizerForm(data))
        {
            windowService.ShowDialog(displayForm);
        }
    }

    /// <summary>
    /// Tests the visualizer by hosting it outside of the debugger.
    /// </summary>
    /// <param name="objectToVisualize">The object to display in the visualizer.</param>
    public static void TestShowVisualizer(object objectToVisualize)
    {
        VisualizerDevelopmentHost visualizerHost = new VisualizerDevelopmentHost(objectToVisualize, typeof(DebuggerSide));
        visualizerHost.ShowVisualizer();
    }
}

VisualizerForm is the custom form with extra controls etc... when I build the project and put the dll in the My Documents/Visual Studio 11/Visualizers folder, and restart visual studio, I can indeed see the debugger appearing under the looking glass icon when a breakpoint is hit for a string object. Woohoo! So far so good.

Now I would like to, instead of visualizing string, visualize string[] or IEnumerable<string> or a similar object. However when I change the assembly attribute to IEnumerable<string>, this is not working, there is not even a looking glass icon displayed on the IEnumerable objects.

UPDATE
I can get it to work by changing the TargetType to List<> and then checking if I can cast to List<string>. However, this means I have to cast all my objects I want to debug to List and can't use IEnumerable<> or string[]

like image 917
RoelF Avatar asked Aug 28 '12 13:08

RoelF


2 Answers

Visualizers are documented as

Support for generic types is limited. You can write a visualizer for a target that is a generic type only if the generic type is an open type.

Which means you cannot write a visualizer that uses a closed constructed type like IEnumerable<string>.

Have you tried setting the target type to IEnumerable<> then checking to see if the elements are of type string?

like image 166
Peter Ritchie Avatar answered Nov 04 '22 22:11

Peter Ritchie


See the 'Objects that can have debugger visualizers' section here: https://rapiddevbookcode.codeplex.com/wikipage?title=EnumerableDebugVisualizer

My Enumerable Debugger Visualizer will work with an IEnumerable if the underlying concrete type registered with it, you could look at the registration code to get yours working.

like image 23
Jeremy Thomas Avatar answered Nov 04 '22 22:11

Jeremy Thomas