1

When using [DebuggerDisplay("{OneLineAddress}")] on the debugger proxy'd class, it does not seem to work. Is there something I'm doing wrong? Or some way around this without adding code to the original class?

[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; }
        }
    }
}
playerone
  • 127
  • 1
  • 8

2 Answers2

0

DebuggerDisplay attribute should be used on class, not on proxy. To achieve same effect, as you try accomplish, you can just add DebuggerDisplayAttribute on your class (without 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;
    }
}

Text nq in Street, City and State removes quotes from properties.

0

The [DebuggerDisplay("{OneLineAddress}")] works only on specific class instance. To see the result in your sample code, you need to create instane of AddressProxy class.

To see the "one line address" on the Address class you can use

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

Or:

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

I personally recommend the ToString() method, because using in list and arrays this displays the correct state one line address...

The DebuggerTypeProxy should be used for list, because it is used in debugger after expanding the current instance. To example see http://msdn.microsoft.com/en-us/library/system.diagnostics.debuggertypeproxyattribute%28v=vs.110%29.aspx

Julo
  • 1,102
  • 1
  • 11
  • 19