1

在调试器代理类上使用 [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; }
        }
    }
}
4

2 回答 2

0

仅适用于特定的 [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/en-us/library/system.diagnostics.debuggertypeproxyattribute%28v=vs.110%29.aspx

于 2014-06-30T09:45:09.123 回答
0

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

Street、City 和 State 中的文本nq会从属性中删除引号。

于 2013-05-22T15:40:59.060 回答