在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
使用 DebuggerBrowsable 特性可以设置调式运行时属性在Watch窗口中的可视状态。
Person
{ private string _lastName = string.Empty; private string _firstName = string.Empty; private int _age = 0; private List<Person> _children = new List<Person>(); private List<string> _pets = new List<string>(); private List<string> _cars = new List<string>(); public string LastName { get { return _lastName; } set { _lastName = value; } } public string FirstName { get { return _firstName; } set { _firstName = value; } } public int Age { get { return _age; } set { _age = value; } } [DebuggerBrowsable(DebuggerBrowsableState.Collapsed)] public List<Person> Children { get{return _children;} } [DebuggerBrowsable(DebuggerBrowsableState.Never)] public List<string> Pets { get { return _pets; } } [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] public List<string> Cars { get { return _cars; } } public Person() { } public Person(string lastName, string firstName, int age) { _lastName = lastName; _firstName = firstName; _age = age; } }
通过Type Proxy简化对象查看 使用 [DebuggerTypeProxy(typeof(XXXProxy))] 特性标记要简化查看的类,XXXProxy 是新创建的类,目的只是为了定义要在Watch窗口显示的属性
PersonProxy
{ private Person _person; public PersonProxy(Person person) { _person = person; } public string Children { get { string[] children = new string[_person.Children.Count]; for(int i = 0; i<_person.Children.Count;i++) { children[i] = _person.Children[i].FirstName; } return string.Join(", ",children); } } public string Pets { get { return string.Join(", ", _person.Pets.ToArray()); } } public string Cars { get { return string.Join(", ", _person.Cars.ToArray()); } } public string Person { get { return "Name = " + _person.FirstName + " " + _person.LastName + ", Age = " + _person.Age; } } }
显示直观的Watch值
,
Type = "Alcohol Age Check")] public string AlcoholAgeCheck { get { return string.Empty; } } 如上代码,可以通过 DebuggerDisplay 特性使用条件判断来自定义Watch窗口显示的信息
ValidateObject()
{ List<string> violations = new List<string>(); //make sure that a first name has been specified if (0 == _person.FirstName.Length) { violations.Add("FirstName is empty"); } //make sure that a last name has been specified if (0 == _person.LastName.Length) { violations.Add("LastName is empty"); } //make sure that age is greater than 0 if (0 > _person.Age) { violations.Add("Age is less than 0"); } //put additional validations here //return results to DebuggerDisplay attribute return string.Join(", ", violations.ToArray()); } [DebuggerDisplay("{ValidateObject()}")] public string BusinessRulesViolations { get { return string.Empty; } }
|
请发表评论