在调试器代理类上使用[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; }
}
}
}
答案 0 :(得分: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;
}
}
街道,城市和州中的文字nq
会删除属性中的引号。
答案 1 :(得分: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