簡體   English   中英

c#是否有相當於格式化類成員的DebuggerDisplay?

[英]c# is there a DebuggerDisplay equivalent to format class members?

DebuggerDisplay 屬性允許為整個類顯示自定義“值”或解釋。 這很好,但是是否有可能強制將標准類型成員(即 UInt32)也顯示為十六進制值?

我想要這個有兩個原因

  • 我的一些成員僅在十六進制中有意義(地址、位掩碼)
  • 十六進制格式在 C# IDE 中是全局的,所以我必須經常手動切換

[DebuggerDisplay("{_value,h}")] 
public abstract class DataField
{
  [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  private UInt32 _value;
            
  // display this member in debugger permanent as HEX 
  public UInt32 ConstMask { get; set; } 
}

我看到的一個選擇是將 ConstMask 聲明為一個類並應用 DebuggerDisplay 格式,但這會影響我的性能,我想這不是一個好的選擇,僅用於調試目的。

提前感謝您的提示,

當您需要更多控制/代碼來格式化調試器顯示時,可以使用nq和用於返回字符串的屬性名稱。 像這樣:

[DebuggerDisplay("{DebuggerDisplay,nq}")]
public abstract class DataField
{
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private UInt32 _value;

    // display this member in debugger permanent as HEX 
    public UInt32 ConstMask { get; set; }

    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private string DebuggerDisplay =>
        $"{this.GetType().Name} 0x{this._value:X8}";
}

您正在尋找DebuggerTypeProxyAttribute 此屬性允許您定義一個可以擴展以顯示所有屬性的類型。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM