[英]How to use Visual Studio Text Visualizer for custom types?
在Visual Studio 2015(以及某些舊版本)中,在調試C#代碼時,可以通過帶有放大鏡圖標的下拉列表在各種可視化工具(文本,XML,HTML,JSON)中顯示string
變量的值。 這也適用於某些非字符串類型,例如System.Xml.Linq.XElement
。 是否可以使用這些內置可視化工具來顯示我自己的自定義類型變量的值?
語境:
我需要能夠快速檢查復雜自定義類型的狀態,該類型只能在多行文本環境中可接受地顯示。
如果我正確理解您的問題,那么您可以使用DebuggerTypeProxy實現您的目標。 它會導致調試器在您檢查復雜類型的對象時創建和顯示代理對象。
在下面的示例中,代理對象包含一個(多行)字符串屬性,您可以使用文本可視化工具查看該屬性。 如果你仍然需要查看底層對象本身,那么這就是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;
}
}
是的你可以。 其中一個選項是使用DebuggerDisplayAttribute
調試器顯示屬性允許指定並最好地理解該類型的運行時行為的類型的開發人員還指定在調試器中顯示該類型時的類型。
[DebuggerDisplay("The count value in my class is: {count}")]
class MyClass
{
public int count { get; set; }
}
編輯:解釋后,我明白你想要什么。 可以自定義多行可視化工具,但你可能不喜歡這樣做:)
Microsoft.VisualStudio.DebuggerVisualizers.dll
的引用。 我在Add Reference - > Assemblies - > Extensions列表中找到了它 Serializible
以下是示例代碼:
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());
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.