How to specify order of debugger visualizers in Visual Studio

C#Visual StudioDebuggervisualizer

C# Problem Overview


I've been working on a debugger visualizer for Visual Studio for some time and while the actual visualizer works fine. The problem is that it always places itself at the top of the visualizer list when examining a variable which really annoys some of the users who rather have Text as the top one (since the top one is also default when opening VS).

enter image description here

I can't find any support for this on DialogDebuggerVisualizer or DebuggerVisualizerAttribute which were my first thoughts so I've been scouring SO/MSDN/Google for information on how to affect the sort order of the visualizers (preferably to put mine last in the list) but to no avail.

Below is how I register my visualizer, it then just shows a form based on the value that is being visualized.

using Microsoft.VisualStudio.DebuggerVisualizers;

[assembly: System.Diagnostics.DebuggerVisualizer(
    typeof(Shorthand.VSAddins.JsonVisualizer.JsonVisualizer),
    typeof(VisualizerObjectSource),
    Target = typeof(string),
    Description = "Json Visualizer")]
namespace Shorthand.VSAddins.JsonVisualizer
{
    public class JsonVisualizer : DialogDebuggerVisualizer
    {
        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            var json = objectProvider.GetObject() as string;

            var form = new VisualizerForm { Json = json };
            windowService.ShowDialog(form);
        }
    }
}

Does anyone know if it is possible to affect the order of the visualizers or should I just let it be?

C# Solutions


Solution 1 - C#

I don't think there is a solution. But there is a workaround:

Define your own Text Visualizer and put appropriate DebuggerVisualizer attribute before the attribute of your JsonVisualizer. The result will be that string will be readable by default and Json Visualizer can be chosen. A window with a multi-line textbox is not too much work.

It is probably not even necessary to write visualizer. It should be possible to use internal one but I don't know its name (https://stackoverflow.com/questions/18912035/which-class-is-used-for-text-visualizer).

Solution 2 - C#

It will always appear first, by design. The under the hood cast has found the best match for the variable it is reflecting on.

however, you could do either of two things. You could make the visualizer only appear when the sting contains ':' Or you could use reflection to reorder the visualisers by adding them to the end of the collection in the order you want, then removing the originals from the collection. For the latter you will most likely have to change the collection from readonly to writable. Via reflection.

There is no reliable source to draw on other than your will to succeed.

Solution 3 - C#

I guess that VS 'under the hood' can distinguish between type of string and type of xml quite easily, but Xml is just a string too, so a key question here would be, how does VS tell the difference between the two?

Could you dissect the VS XML visualizer to see how it works (even if you have to use reflector on the DLL to do it, you might get to see the method that works it out)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKarl-Johan SjögrenView Question on Stackoverflow
Solution 1 - C#IvanHView Answer on Stackoverflow
Solution 2 - C#Aaron HopkinsView Answer on Stackoverflow
Solution 3 - C#FlemGremView Answer on Stackoverflow