Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Visual Studio Text Visualizer for custom types?

In Visual Studio 2015 (and in some older versions) when debugging C# code it's possible to display the values of the string variables in various visualizers (Text, XML, HTML, JSON) via a drop-down list with a magnifying glass icon. This also works for some non-string types, for example, System.Xml.Linq.XElement. Is it possible to use these build-in visualizers to display the value of a variable of my own custom type?

Context:

I need to be able to quickly check the state of a complicated custom type that can be acceptably visualized only in a multi-line text environment.

like image 693
user98418468459 Avatar asked Jan 20 '17 13:01

user98418468459


People also ask

How do I use Debug Visualizer?

Go to Run > Start Debugging and select an environment (e.g Node. js (preview) ). From the command-palette, select Debug Visualizer: New View .


Video Answer


2 Answers

If I understand your question correctly, then you can achieve what you're after with a DebuggerTypeProxy. It causes the debugger to create and display a proxy object whenever you're inspecting objects of your complex type.

In the example below the proxy object contains a (multi-line) string property that you can view with the text visualizer. If you still need to look at the underlying object itself, then that's what the Raw view button is for.

[DebuggerTypeProxy(typeof(ComplexTypeProxy))]
class ComplexType
{
    // complex state
}

class ComplexTypeProxy
{
    public string Display
    {
        get { return "Create a multi-line representation of _content's complex state here."; }
    }

    private ComplexType _content;

    public ComplexTypeProxy(ComplexType content)
    {
        _content = content;
    }
}
like image 155
Pieter Witvoet Avatar answered Sep 21 '22 19:09

Pieter Witvoet


Yes you can. One of options is to use DebuggerDisplayAttribute

Debugger display attributes allow the developer of the type, who specifies and best understands the runtime behavior of that type, to also specify what that type will look like when it is displayed in a debugger.

[DebuggerDisplay("The count value in my class is: {count}")]
class MyClass
{
   public int count { get; set; }
}

EDIT: After explanation I understood what you want. It is possible to do your custom multi-line visualiser, but you probably don't like the way of doing it :)

  1. You need to add the reference to Microsoft.VisualStudio.DebuggerVisualizers.dll. I found it in Add Reference -> Assemblies -> Extensions list
  2. Your need to create new class and inherit DialogDebuggerVisualizer class. Override Show method and display the required content.
  3. Mark your class as Serializible
  4. Add reference to your custom Visualizer

Here is the sample code:

using System.Windows.Forms;
using Microsoft.VisualStudio.DebuggerVisualizers;
[assembly: DebuggerVisualizer(typeof(MyClassVisualizer), Target = typeof(MyClass), Description = "My Class Visualizer")]

namespace MyNamespace
{
    [Serializable]
    public class MyClass
    {
        public int count { get; set; } = 5;
    }

    public class MyClassVisualizer : DialogDebuggerVisualizer
    {
        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            MyClass myClass = objectProvider.GetObject() as MyClass;

            if (objectProvider.IsObjectReplaceable && myClass != null)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Here is");
                sb.AppendLine("your multi line");
                sb.AppendLine("visualizer");
                sb.AppendLine($"of MyClass with count = {myClass.count}");

                MessageBox.Show(sb.ToString());
            }
        }
    }
}

Then you will see magnifier and when you click it the result will look like this: enter image description here

like image 45
Renatas M. Avatar answered Sep 22 '22 19:09

Renatas M.