簡體   English   中英

DebuggerTypeProxy類上的DebuggerDisplay屬性

[英]DebuggerDisplay attribute on a DebuggerTypeProxy Class

在調試器代理的類上使用[DebuggerDisplay(“ {OneLineAddress}”))時,它似乎不起作用。 我做錯了什么嗎? 還是在不向原始類添加代碼的情況下解決此問題?

[DebuggerTypeProxy(typeof(AddressProxy))]
class Address
{
    public int Number { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public int Zip { get; set; }

    public Address(int number, string street, string city, string state, int zip)
    {
        Number = number;
        Street = street;
        City = city;
        State = state;
        Zip = zip;
    }

    [DebuggerDisplay("{OneLineAddress}")] // doesn't seem to work on proxy
    private class AddressProxy
    {
        [DebuggerBrowsableAttribute(DebuggerBrowsableState.Never)]
        private Address _internalAddress;

        public AddressProxy(Address internalAddress)
        {
            _internalAddress = internalAddress;
        }

        public string OneLineAddress
        {
            get { return _internalAddress.Number + " " + _internalAddress.Street + " " + _internalAddress.City + " " + _internalAddress.State + " " + _internalAddress.Zip; }
        }
    }
}

DebuggerDisplay屬性應該在類上使用,而不是在代理上使用。 為了達到相同的效果,您可以嘗試在類上添加DebuggerDisplayAttribute(不使用AddressProxy):

[DebuggerDisplay("{Number} {Street,nq} {City,nq} {State,nq} {Zip}")]
class Address
{
    public int Number { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public int Zip { get; set; }

    public Address(int number, string street, string city, string state, int zip)
    {
        Number = number;
        Street = street;
        City = city;
        State = state;
        Zip = zip;
    }
}

街道,城市和州中的文本nq會刪除屬性中的引號。

[DebuggerDisplay("{OneLineAddress}")]僅適用於特定的類實例。 要在示例代碼中查看結果,您需要創建AddressProxy類的實例。

要查看Address類上的“一行地址”,可以使用

[DebuggerDisplay("{Number} {Street,nq} {City,nq} {State,nq} {Zip}")]
class Address { .... }

要么:

public override string ToString()
{
  return string.Format("{0} {1} {2} {3} {4}", Number, Street, City, State, Zip);
}

我個人建議使用ToString()方法,因為在列表和數組中使用它會顯示正確的狀態一行地址...

DebuggerTypeProxy應該用於列表,因為擴展當前實例后在調試器中使用它。 例如,請參見http://msdn.microsoft.com/zh-cn/library/system.diagnostics.debuggertypeproxyattribute%28v=vs.110%29.aspx

暫無
暫無

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

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