備註
本文專屬於 .NET Framework。 它不適用於較新的 .NET 實作,包括 .NET 6 和更新版本。
調試程式顯示屬性可讓類型的開發人員指定並最瞭解該類型的運行時間行為,以指定在調試程式中顯示該類型時的外觀。 此外,調試器顯示屬性提供的Target
屬性可由無需瞭解原始程式碼的使用者在元件層級套用。 屬性 DebuggerDisplayAttribute 會控制類型或成員在調試程式變數視窗中的顯示方式。 屬性 DebuggerBrowsableAttribute 會決定欄位或屬性在調試程式變數視窗中的顯示方式和方式。
DebuggerTypeProxyAttribute 屬性指定類型的替代類型或代理,並在偵錯工具視窗中改變類型的顯示方式。 當您檢視具有 Proxy 或替代類型的變數時,Proxy 會代表調試程式顯示視窗中的原始類型。 調試程式變數視窗只會顯示 Proxy 類型的公用成員。 不會顯示私人成員。
使用 DebuggerDisplayAttribute
DebuggerDisplayAttribute 構造函式具有一個參數:這是一個字串,用於顯示在該型別實例的值欄位中。 此字串可以包含大括號 ({ 和 })。 在一對大括號內的文字會被評估為一個表達式。 例如,下列 C# 程式碼在展開 MyHashtable
實例的調試顯示時,選取加號(+)會顯示「Count = 4」。
[DebuggerDisplay("Count = {count}")]
class MyHashtable
{
public int count = 4;
}
不會處理套用至表達式中所參考屬性的屬性。 對於 C# 編譯器,允許使用的一般表達式只允許隱式地存取目前目標型別的當前實例的參考。 表達式有限;無法存取別名、局部變數或指標。 在 C# 程式代碼中,您可以在大括弧之間使用一般表達式,該大括弧只能隱含存取 this
目標型別之目前實例的指標。
例如,如果 C# 物件已覆寫ToString()
,偵錯工具會呼叫覆寫,並顯示其結果,而不是標準{<typeName>}.
。因此,如果您已覆寫ToString()
,則不需要使用DebuggerDisplayAttribute。 如果您使用這兩者, DebuggerDisplayAttribute 屬性的優先順序高於 ToString()
覆寫。
使用 DebuggerBrowsableAttribute 屬性
DebuggerBrowsableAttribute將套用至欄位或屬性,以指定在調試程式視窗中顯示欄位或屬性的方式。 此屬性的建構函式會採用其中 DebuggerBrowsableState 一個列舉值,指定下列其中一個狀態:
Never 表示成員不會顯示在數據視窗中。 例如,使用這個值於DebuggerBrowsableAttribute欄位上會從階層中移除該欄位;當您點擊類型實例的加號(+)以展開封入類型時,該欄位將不顯示。
Collapsed 表示成員已顯示,但預設不自動展開。 此為預設行為。
RootHidden 表示成員本身未顯示,但如果它是陣列或集合,則會顯示其組成物件。
備註
在 .NET Framework 2.0 版中,Visual Basic 不支援 DebuggerBrowsableAttribute。
以下程式碼範例顯示如何使用 DebuggerBrowsableAttribute 來避免隨後的屬性出現在類別的偵錯視窗中。
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public static string y = "Test String";
使用 DebuggerTypeProxy
DebuggerTypeProxyAttribute當您需要大幅且從根本上變更類型的偵錯檢視時,請使用 屬性,但不要變更類型本身。 DebuggerTypeProxyAttribute屬性可用來指定類型的顯示代理,讓開發人員能夠針對類型量身打造檢視。 這個屬性,例如 DebuggerDisplayAttribute,可以在元件層級使用,在此情況下, Target 屬性會指定要使用 Proxy 的類型。 建議的使用方式是,此屬性指定應用於該型別內部的私有巢狀類型。 表達式評估工具支援類型檢視器,當顯示類型時會檢查此屬性。 如果找到屬性,表達式評估工具會以顯示 Proxy 類型替換套用屬性的類型。
DebuggerTypeProxyAttribute當 存在 時,調試程式變數視窗只會顯示 Proxy 類型的公用成員。 不會顯示私人成員。 數據窗口的行為不會因屬性增強的視圖而改變。
為了避免不必要的效能損失,顯示代理的屬性在物件展開之前不會被處理,展開的方式可以是使用者在資料視窗中點擊類型旁邊的加號(+),或者是通過應用 DebuggerBrowsableAttribute 屬性。 因此,建議不將任何屬性套用至顯示類型。 屬性可以且應該套用在顯示類型的主體內。
下列程式代碼範例示範如何使用 DebuggerTypeProxyAttribute 來指定要當做調試程序顯示 Proxy 的型別。
[DebuggerTypeProxy(typeof(HashtableDebugView))]
class MyHashtable : Hashtable
{
private const string TestString =
"This should not appear in the debug window.";
internal class HashtableDebugView
{
private Hashtable hashtable;
public const string TestStringProxy =
"This should appear in the debug window.";
// The constructor for the type proxy class must have a
// constructor that takes the target type as a parameter.
public HashtableDebugView(Hashtable hashtable)
{
this.hashtable = hashtable;
}
}
}
範例
說明
下列程式代碼範例可以在 Visual Studio 中檢視,以查看套用 DebuggerDisplayAttribute、 DebuggerBrowsableAttribute和 DebuggerTypeProxyAttribute 屬性的結果。
程式碼
using namespace System;
using namespace System::Collections;
using namespace System::Diagnostics;
using namespace System::Reflection;
ref class HashtableDebugView;
[DebuggerDisplay("{value}", Name = "{key}")]
ref class KeyValuePairs
{
private:
IDictionary^ dictionary;
Object^ key;
Object^ value;
public:
KeyValuePairs(IDictionary^ dictionary, Object^ key, Object^ value)
{
this->value = value;
this->key = key;
this->dictionary = dictionary;
}
};
[DebuggerDisplay("Count = {Count}")]
[DebuggerTypeProxy(HashtableDebugView::typeid)]
ref class MyHashtable : Hashtable
{
private:
static const String^ TestString = "This should not appear in the debug window.";
internal:
ref class HashtableDebugView
{
private:
Hashtable^ hashtable;
public:
static const String^ TestString = "This should appear in the debug window.";
HashtableDebugView(Hashtable^ hashtable)
{
this->hashtable = hashtable;
}
[DebuggerBrowsable(DebuggerBrowsableState::RootHidden)]
property array<KeyValuePairs^>^ Keys
{
array<KeyValuePairs^>^ get()
{
array<KeyValuePairs^>^ keys = gcnew array<KeyValuePairs^>(hashtable->Count);
IEnumerator^ ie = hashtable->Keys->GetEnumerator();
int i = 0;
Object^ key;
while (ie->MoveNext())
{
key = ie->Current;
keys[i] = gcnew KeyValuePairs(hashtable, key, hashtable[key]);
i++;
}
return keys;
}
}
};
};
public ref class DebugViewTest
{
private:
// The following constant will appear in the debug window for DebugViewTest.
static const String^ TabString = " ";
public:
// The following DebuggerBrowsableAttribute prevents the property following it
// from appearing in the debug window for the class.
[DebuggerBrowsable(DebuggerBrowsableState::Never)]
static String^ y = "Test String";
static void Main()
{
MyHashtable^ myHashTable = gcnew MyHashtable();
myHashTable->Add("one", 1);
myHashTable->Add("two", 2);
Console::WriteLine(myHashTable->ToString());
Console::WriteLine("In Main.");
}
};
int main()
{
DebugViewTest::Main();
}
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
class DebugViewTest
{
// The following constant will appear in the debug window for DebugViewTest.
const string TabString = " ";
// The following DebuggerBrowsableAttribute prevents the property following it
// from appearing in the debug window for the class.
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public static string y = "Test String";
static void Main()
{
MyHashtable myHashTable = new MyHashtable();
myHashTable.Add("one", 1);
myHashTable.Add("two", 2);
Console.WriteLine(myHashTable.ToString());
Console.WriteLine("In Main.");
}
}
[DebuggerDisplay("{value}", Name = "{key}")]
internal class KeyValuePairs
{
private IDictionary dictionary;
private object key;
private object value;
public KeyValuePairs(IDictionary dictionary, object key, object value)
{
this.value = value;
this.key = key;
this.dictionary = dictionary;
}
}
[DebuggerDisplay("Count = {Count}")]
[DebuggerTypeProxy(typeof(HashtableDebugView))]
class MyHashtable : Hashtable
{
private const string TestString = "This should not appear in the debug window.";
internal class HashtableDebugView
{
private Hashtable hashtable;
public const string TestString = "This should appear in the debug window.";
public HashtableDebugView(Hashtable hashtable)
{
this.hashtable = hashtable;
}
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public KeyValuePairs[] Keys
{
get
{
KeyValuePairs[] keys = new KeyValuePairs[hashtable.Count];
int i = 0;
foreach(object key in hashtable.Keys)
{
keys[i] = new KeyValuePairs(hashtable, key, hashtable[key]);
i++;
}
return keys;
}
}
}
}
Imports System.Collections
Imports System.Diagnostics
Imports System.Reflection
Class DebugViewTest
' The following constant will appear in the debug window for DebugViewTest.
Const TabString As String = " "
' The following DebuggerBrowsableAttribute prevents the property following it
' from appearing in the debug window for the class.
<DebuggerBrowsable(DebuggerBrowsableState.Never)> _
Public Shared y As String = "Test String"
Shared Sub Main()
Dim myHashTable As New MyHashtable()
myHashTable.Add("one", 1)
myHashTable.Add("two", 2)
Console.WriteLine(myHashTable.ToString())
Console.WriteLine("In Main.")
End Sub
End Class
<DebuggerDisplay("{value}", Name:="{key}")> _
Friend Class KeyValuePairs
Private dictionary As IDictionary
Private key As Object
Private value As Object
Public Sub New(ByVal dictionary As IDictionary, ByVal key As Object, ByVal value As Object)
Me.value = value
Me.key = key
Me.dictionary = dictionary
End Sub
End Class
<DebuggerDisplay("Count = {Count}"), DebuggerTypeProxy(GetType(MyHashtable.HashtableDebugView))> _
Class MyHashtable
Inherits Hashtable
Private Const TestString As String = "This should not appear in the debug window."
Friend Class HashtableDebugView
Private hashtable As Hashtable
Public Shared TestString As String = "This should appear in the debug window."
Public Sub New(ByVal hashtable As Hashtable)
Me.hashtable = hashtable
End Sub
<DebuggerBrowsable(DebuggerBrowsableState.RootHidden)> _
ReadOnly Property Keys as KeyValuePairs()
Get
Dim nkeys(hashtable.Count - 1) As KeyValuePairs
Dim i as Integer = 0
For Each key As Object In hashtable.Keys
nkeys(i) = New KeyValuePairs(hashtable, key, hashtable(key))
i = i + 1
Next
Return nkeys
End Get
End Property
End Class
End Class