How to use Visual Visual Textizer for custom types?

In Visual Studio 2015 (and in some older versions), when debugging C # code, you can display the values โ€‹โ€‹of string variables in various visualizers (text, XML, HTML, JSON) through a drop-down list with a magnifying glass icon. This also works for some non-line types, for example, System.Xml.Linq.XElement . Can these built-in visualizers be used to display the value of a variable of my own type?

Context:

I need to be able to quickly check the status of a complex custom type that can only be visually visualized in a multi-line text environment.

+5
source share
2 answers

If I understand your question correctly, you can achieve what you need with DebuggerTypeProxy . This forces the debugger to create and display proxy objects whenever you scan objects of a complex type.

In the example below, the proxy object contains a (multi-line) string property that you can view using a text renderer. If you still need to look at the main object, then what is the Raw view button for?

 [DebuggerTypeProxy(typeof(ComplexTypeProxy))] class ComplexType { // complex state } class ComplexTypeProxy { public string Display { get { return "Create a multi-line representation of _content complex state here."; } } private ComplexType _content; public ComplexTypeProxy(ComplexType content) { _content = content; } } 
+3
source

Yes, you can. One option is to use DebuggerDisplayAttribute

The debugger display attributes allow the developer of a type that defines and better understands the runtime behavior of this type, also indicates how this type will look when it is displayed in the debugger.

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

EDIT: After explaining, I realized what you want. You can make your own multi-line renderer, but you probably don't like the way it is done :)

  • You need to add the link to Microsoft.VisualStudio.DebuggerVisualizers.dll . I found it in the list Add Link โ†’ Assemblies โ†’ Extensions
  • You need to create a new class and inherit the DialogDebuggerVisualizer class. Override the Show method and display the desired content.
  • Mark your class as Serializible
  • Add a link to your custom document document

Here is a 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 a magnifying glass, and when you click it, the result will look like this: enter image description here

0
source

Source: https://habr.com/ru/post/1263100/


All Articles