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.
Go to Run > Start Debugging and select an environment (e.g Node. js (preview) ). From the command-palette, select Debug Visualizer: New View .
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 :)
Microsoft.VisualStudio.DebuggerVisualizers.dll
. I found it in Add Reference -> Assemblies -> Extensions listSerializible
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With