【问题标题】:DebuggerDisplay attribute on a DebuggerTypeProxy ClassDebuggerTypeProxy 类上的 DebuggerDisplay 属性
【发布时间】:2013-05-22 15:19:40
【问题描述】:

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

【问题讨论】:

    标签: c# .net debugging debuggerdisplay


    【解决方案1】:

    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 会从属性中删除引号。

    【讨论】:

    • 是的,但我真正想要的是从 Proxy 类中为 DebuggerDisplay 读取一些属性。
    【解决方案2】:

    [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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-22
      • 2011-03-24
      • 2011-05-27
      • 1970-01-01
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多