共用方式為


使用 DebuggerDisplay 屬性

更新:2007 年 11 月

這個主題適用於:

版本

Visual Basic

C#

C++

Web Developer

Express 版

標題適用於 標題適用於 標題適用於 標題適用於

Standard 版

標題適用於

標題適用於

標題適用於

標題適用於

Pro/Team 版

標題適用於

標題適用於

標題適用於

標題適用於

表格圖例:

標題適用於

套用

標題不適用於

不套用

預設會套用主題但隱藏命令

預設隱藏的命令。

DebuggerDisplay 屬性 (System.Diagnostics.DebuggerDisplayAttribute) 控制偵錯工具變數視窗中顯示類別或欄位的方式。這個屬性可以套用至:

  • 類別

  • 結構

  • 委派

  • 列舉

  • 欄位

  • 屬性

  • 組件

DebuggerDisplay 屬性有單一引數,是顯示在型別執行個體之值欄中的字串。這個字串可以包含大括號 ({ 和 })。在括號內的文字將會被評估為欄位、屬性或方法。

在 C# 程式碼中,可以在大括號之間使用一般運算式。運算式只能夠隱含存取目標型別之目前執行個體的 this 指標。運算式無法存取別名、區域變數或指標。如果運算式參考屬性 (Property),則不會處理這些屬性中的屬性 (Attribute)。

如果 C# 物件具有覆寫的 ToString(),則偵錯工具會呼叫覆寫並顯示它的結果,而不是標準的 {<typeName>}。因此,如果您具有覆寫的 ToString(),就不需要使用 DebuggerDisplay。如果兩者都使用,DebuggerDisplay 屬性則會優先於 ToString() 覆寫。

偵錯工具是否會評估這個隱含 ToString() 呼叫,是依照 [選項] 對話方塊中的使用者設定而定 ([一般] 頁面、[偵錯] 分類)。Visual Basic 並未實作這個隱含 ToString() 評估。

下表顯示 DebuggerDisplay 屬性的某些可能用法和範例輸出。

屬性

輸出顯示在 [值] 欄中

[DebuggerDisplay("x = {x} y = {y}")]

使用搭配與 x 和 y 欄位的型別上。

x = 5 y = 18

[DebuggerDisplay("String value is {getString()}")]

參數語法會因語言而有所不同。因此,請小心使用。

String value is [5, 6, 6]

[DebuggerDisplay("Object {count - 2}: {(flag) ? \"yes\" : \"no\"}")]

運算式語法會因語言而有所不同。因此,請小心使用。

Object 6: yes

[DebuggerDisplay("Last = {_lastName,nq} {_var == null ? \"\" : \"First = \" + _firstName,nq}")]

,nq 會除去引號。

如果有姓氏

Last = lastname First = firstname

否則就是:

Last = lastname

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 9.0\Common7\IDE\PublicAssemblies 中的 Microsoft.VisualStudio.DebuggerVisualizers.dll。而 autoexp.cs 檔則位在 My Documents/Visual Studio 9.0/Visualizers 中。

範例

下列程式碼範例顯示如何同時使用 DebuggerDisplayDebuggerBrowseableDebuggerTypeProxy。在偵錯工具變數視窗中檢視時,例如 [監看式] 視窗,其會以類似下列方式展開:

名稱

類型

鍵值

"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("Count = {hashtable.Count}")]
[DebuggerTypeProxy(typeof(HashtableDebugView))]
class MyHashtable
{
    public Hashtable hashtable;

    public MyHashtable()
    {
        hashtable = new Hashtable();  
    }

    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;
            }
        }
    }
}

請參閱

概念

顯示自訂資料型別

使用偵錯工具顯示屬性增強偵錯功能

參考

使用 DebuggerTypeProxy 屬性