DebuggerBrowsable 属性用来指定是否在调试器变量窗口中显示成员以及如何显示成员,比如你有下面的代码:
public class VehicleSeries
{
public string Name
{
get;
set;
}
public List<string> Vehicles
{
get;
set;
}
}
{
public string Name
{
get;
set;
}
public List<string> Vehicles
{
get;
set;
}
}
当你调试代码时,是以下面的方式显示的:
但是我们只是想看Name、Vehicles属性的值而已,并不想看到私有变量的值。下面我们就利用DebuggerBrowsable特性来完成这个功能,改动后的代码如下:
public class VehicleSeries
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private List<string> _vehicles = new List<string>();
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public List<string> Vehicles
{
get { return _vehicles; }
set { _vehicles = value; }
}
}
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private List<string> _vehicles = new List<string>();
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public List<string> Vehicles
{
get { return _vehicles; }
set { _vehicles = value; }
}
}
再调试代码时,就会以如下的方式显示了:(是不是简洁多了呢? :))