使用 DebuggerDisplay 屬性
DebuggerDisplay 屬性 (System.Diagnostics.DebuggerDisplayAttribute) 控制偵錯工具變數視窗中顯示類別或欄位的方式。 這個屬性可以套用至:
類別
Structs
委派
列舉
欄位
屬性
組件
DebuggerDisplay 屬性有單一引數,是顯示在型別執行個體之值欄中的字串。 這個字串可以包含大括號 ({ 和 })。 在括號內的文字將會被評估為欄位、屬性或方法。
如果 C# 物件具有覆寫的 ToString(),則偵錯工具會呼叫覆寫並顯示它的結果,而不是標準的 {<typeName>}。 因此,如果您具有覆寫的 ToString(),就不需要使用 DebuggerDisplay。 如果兩者都使用,DebuggerDisplay 屬性則會優先於 ToString() 覆寫。
偵錯工具是否會評估這個隱含 ToString() 呼叫,是依照 [選項] 對話方塊中的使用者設定而定 ([一般] 頁面、[偵錯] 分類)。 Visual Basic 並未實作這個隱含 ToString() 評估。
重要
如果在 [工具選項] 對話方塊中選取 [在變數視窗中顯示物件的原始結構]核取方塊,則會忽略 DebuggerDisplay 屬性。
下表顯示 DebuggerDisplay 屬性的某些可能用法和範例輸出。
屬性 |
輸出顯示在 [值] 欄中 |
---|---|
[DebuggerDisplay("x = {x} y = {y}")] 使用搭配與 x 和 y 欄位的型別上。 |
x = 5 y = 18 |
[DebuggerDisplay("String value is {getString()}")]參數語法會因語言而有所不同。 因此,請小心使用。 |
String value is [5, 6, 6] |
DebuggerDisplay 也可以接受具名參數。
參數 |
用途 |
---|---|
Name, Type |
這些參數會影響變數視窗的 [名稱] 和 [型別] 欄 (它們可以設為字串所使用的語法與建構函式一樣) 過度使用這些參數或無效,就會造成混淆的輸出。 |
Target, TargetTypeName |
在組件層級使用屬性時,請指定目標型別。 |
![]() |
---|
autoexp.cs 檔會在組件層級使用 DebuggerDisplay 屬性。autoexp.cs 檔決定 Visual Studio 用於 C# 變數的預設展開 (Expansion)。您可以查看 autoexp.cs 檔取得 DebuggerDisplay 屬性的使用範例,也可以修改和編譯 autoexp.cs 檔來變更預設展開。請務必要先備份 autoexp.cs 檔,再進行修改。您必須參考 \Program Files\Microsoft Visual Studio 11.0\Common7\IDE\PublicAssemblies 中的 Microsoft.VisualStudio.DebuggerVisualizers.dll。而 autoexp.cs 檔則位在 My Documents/Visual Studio 2012/Visualizers 中。 |
在 DebuggerDisplay 中使用運算式
雖然您可以在 DebuggerDisplay 屬性中以大括弧括住一般運算式,但不建議這種做法。
DebuggerDisplay 中一般運算式只能隱含存取目標型別目前的執行個體之 this指標。 運算式無法存取別名、區域變數或指標。 如果運算式參考屬性 (Property),則不會處理這些屬性中的屬性 (Attribute)。 例如,如果欄位count是 8,C# 程式碼[DebuggerDisplay("Object {count - 2}" 將顯示Object 6。
在 DebuggerDisplay 中使用運算式可能導致下列問題:
評估運算式是偵錯程式中最複雜的運算,每次顯示運算式時都會進行評估。 這可能會在逐步執行程式碼時導致效能問題。 例如,若元素的數目很大,用於顯示集合或清單中的值的複雜運算式可能非常慢。
運算式由使用目前堆疊框架語言的運算式評估工具評估,而非由撰寫運算式的語言的評估工具來評估。 這可能會在語言不同時導致不可預測的結果。
評估運算式可能會變更應用程式的狀態。 例如,設定屬性值的運算式會改變執行程式碼中的屬性值。
減少運算式評估可能出現的問題的其中一種方法是建立執行操作並傳回字串的私用屬性。 然後 DebuggerDisplay 屬性可以顯示該私用屬性的值。 下列範例實作這個模式:
[DebuggerDisplay("{DebuggerDisplay,nq}")]public sealed class MyClass { public int count { get; set; } public bool flag { get; set; } private string DebuggerDisplay { get{ return string.Format("("Object {0}", count - 2); } }}
範例
下列程式碼範例顯示如何同時使用 DebuggerDisplay、DebuggerBrowseable 和 DebuggerTypeProxy。 在偵錯工具變數視窗中檢視時,例如 [監看式] 視窗,其會以類似下列方式展開:
名稱 |
值 |
型別 |
---|---|---|
機碼 |
"three" |
object {string} |
值 |
3 |
object {int} |
[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;
}
public object Key
{
get { return key; }
set
{
object tempValue = dictionary[key];
dictionary.Remove(key);
key = value;
dictionary.Add(key, tempValue);
}
}
public object Value
{
get { return this.value; }
set
{
this.value = value;
dictionary[key] = this.value;
}
}
}
[DebuggerDisplay("{DebuggerDisplay,nq}")]
[DebuggerTypeProxy(typeof(HashtableDebugView))]
class MyHashtable
{
public Hashtable hashtable;
public MyHashtable()
{
hashtable = new Hashtable();
} private string DebuggerDisplay { get { return "Count = " + hashtable.Count); } }
private class HashtableDebugView
{
private MyHashtable myhashtable;
public HashtableDebugView(MyHashtable myhashtable)
{
this.myhashtable = myhashtable;
}
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public KeyValuePairs[] Keys
{
get
{
KeyValuePairs[] keys = new KeyValuePairs[myhashtable.hashtable.Count];
int i = 0;
foreach (object key in myhashtable.hashtable.Keys)
{
keys[i] = new KeyValuePairs(myhashtable.hashtable, key, myhashtable.hashtable[key]);
i++;
}
return keys;
}
}
}
}