How to use Visual Studio Text Visualizer for custom types?

懵懂的女人 提交于 2019-12-04 04:50:25

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:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!