DebuggerDisplay属性在DebuggerTypeProxy类上
本文关键字:类上 DebuggerTypeProxy 属性 DebuggerDisplay | 更新日期: 2023-09-27 18:14:56
在调试器代理类上使用[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
在Street, City和State中删除属性中的引号。
[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