简体   繁体   English

如何使用Visual Studio Text Visualizer进行自定义类型?

[英]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. 在Visual Studio 2015(以及某些旧版本)中,在调试C#代码时,可以通过带有放大镜图标的下拉列表在各种可视化工具(文本,XML,HTML,JSON)中显示string变量的值。 This also works for some non-string types, for example, System.Xml.Linq.XElement . 这也适用于某些非字符串类型,例如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 . 如果我正确理解您的问题,那么您可以使用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. 如果你仍然需要查看底层对象本身,那么这就是Raw view按钮的用途。

[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 其中一个选项是使用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 . 您需要添加对Microsoft.VisualStudio.DebuggerVisualizers.dll的引用。 I found it in Add Reference -> Assemblies -> Extensions list 我在Add Reference - > Assemblies - > Extensions列表中找到了它
  2. Your need to create new class and inherit DialogDebuggerVisualizer class. 您需要创建新类并继承DialogDebuggerVisualizer类。 Override Show method and display the required content. 覆盖Show方法并显示所需内容。
  3. Mark your class as Serializible 将您的班级标记为Serializible
  4. Add reference to your custom Visualizer 添加对自定义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: 然后你会看到放大镜,当你点击它时,结果将如下所示: 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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