51

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?

Karl-Johan Sjögren
  • 16,544
  • 7
  • 59
  • 68
  • I couldn't see anything obvious but I found a [DebuggerDisplayAttribute](http://msdn.microsoft.com/en-us/library/system.diagnostics.debuggerdisplayattribute.aspx) which may provide some help.. – Sayse Aug 27 '13 at 09:07
  • 2
    The `DebuggerDisplayAttribute` is used to customize how a class is displayed in the debugger and has nothing to do with my problem I'm afraid. See the Remarks-section in the link your posted for a sample of what it does. – Karl-Johan Sjögren Aug 27 '13 at 09:17
  • 1
    Ah apologies, I would only guess then that they are ordered by the order they are loaded in.. Personally I'd say just let it be...There was a IsDefaultAttribute – Sayse Aug 27 '13 at 09:37
  • Yes that is what I'm thinking as well, but I keep hoping that someone has some undocumented magic or something that I could use to fix this for my users. – Karl-Johan Sjögren Aug 27 '13 at 11:17
  • fwiw this is totally infuriating for users of the JsonVisualizer! – Rory Mar 31 '14 at 09:44

3 Answers3

2

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 (Which class is used for "Text Visualizer"?).

Community
  • 1
  • 1
IvanH
  • 5,039
  • 14
  • 60
  • 81
  • 1
    True, but I'm not sure that my users would like it if my addon inserts an extra visualizer like that. A few might accept it, but many would just say that it clutters up the interface by putting in a copy of the default one. But the genral Idea is nice, I might put it in as an option. – Karl-Johan Sjögren Sep 18 '13 at 19:23
0

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.

  • I'm pretty sure that the "under the hood cast" just saw "Hey it's a string, lets get all visualizers with `Target=typeOf(string)`". Do you have any example of how show/hide it depending on the contents of the variable? Unless you mean that the option will always be there but the form won't show? – Karl-Johan Sjögren Sep 14 '13 at 18:11
  • I agree and I offered the solution in my post. Checking for the ':' as part of that string is the key – Aaron Hopkins Sep 14 '13 at 22:51
0

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)

FlemGrem
  • 814
  • 4
  • 9
  • The problem with that is that the xml visualizer as far as I know isn't written in a managed language. It also shows the xml visualiser for any type of string and shows the dialog as well so they aren't doing anything to distinguish the content of the string. Creating a special JsonString-class would make my visualizer quite useless to most people since they wouldn't want to change their codebase to use it, and the string-class is sealed anyway so it can't be extended. This is also quite far from my original question. – Karl-Johan Sjögren Sep 15 '13 at 05:15
  • I too have searched and searched for the answer to this for you. and there is no info out there. if like you say the xml visualiser uses non-managed code, then there is no way to properly reflect the code. I guess your users are just going to have to live with it. its not a biggy, i mean they choose to use the visualizer right. – FlemGrem Sep 16 '13 at 15:28
  • you can't do `jsonString : string`. System.String is sealed – Moslem Ben Dhaou Sep 17 '13 at 10:26