简体   繁体   中英

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.

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;
    }
}

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: 在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM