zoukankan      html  css  js  c++  java
  • ASP.NET 2.0 编程珠玑之五调试已创建好的代码

    5.2 调整对象以便于调试
         有时,并不需要可视化器的强大功能,但仍希望快速、方便地在调试窗口中查看和解释业务对象。System.Diagnostics命名空间有几个不同的特性,使用它们可以修饰类及其它成员并调整对象,以便于调试。实际上,可以降低调试窗口的混乱程度,控制定制业务对象的显示内容。
          下面我们来调试一个Person类,我给出了Person类的完整代码,其中内部类PersonProxy就是为方便调试建立的类:
          
     1using System;
     2using System.Collections.Generic;
     3using System.Text;
     4using System.Diagnostics;
     5
     6namespace MVPHacks.TestDebuggerAttributes
     7{
     8    [DebuggerTypeProxy(typeof(PersonProxy))]
     9    class Person
    10    {
    11        private string _lastName = string.Empty;
    12        private string _firstName = string.Empty;
    13        private int _age = 0;
    14        private List<Person> _children = new List<Person>();
    15        private List<string> _pets = new List<string>();
    16        private List<string> _cars = new List<string>();
    17
    18        public string LastName
    19        {
    20            get return _lastName; }
    21            set { _lastName = value; }
    22        }

    23
    24        public string FirstName
    25        {
    26            get return _firstName; }
    27            set { _firstName = value; }
    28        }

    29
    30        public int Age
    31        {
    32            get return _age; }
    33            set { _age = value; }
    34        }

    35
    36        [DebuggerBrowsable(DebuggerBrowsableState.Collapsed)]
    37        public List<Person> Children
    38        {
    39            get return _children; }
    40        }

    41
    42        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    43        public List<string> Pets
    44        {
    45            get return _pets; }
    46        }

    47
    48        [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
    49        public List<string> Cars
    50        {
    51            get return _cars; }
    52        }

    53
    54        public Person()
    55        {
    56        }

    57
    58        public Person(string lastName, string firstName, int age)
    59        {
    60            _lastName = lastName;
    61            _firstName = firstName;
    62            _age = age;
    63        }

    64    }

    65}

    66

      1using System;
      2using System.Collections.Generic;
      3using System.Text;
      4using System.Diagnostics;
      5using MVPHacks.TestDebuggerAttributes;
      6
      7namespace MVPHacks.TestDebuggerAttributes
      8{
      9    class PersonProxy
     10    {
     11        private Person _person;
     12
     13        public PersonProxy(Person person)
     14        {
     15            _person = person;
     16        }

     17
     18        public string Children
     19        {
     20            get
     21            {
     22                string[] children = new string[_person.Children.Count];
     23                for (int i = 0; i < _person.Children.Count; i++)
     24                {
     25                    children[i] = _person.Children[i].FirstName;
     26                }

     27                return string.Join("", children);
     28            }

     29        }

     30
     31        public string Pets
     32        {
     33            get
     34            {
     35                return string.Join("", _person.Pets.ToArray());
     36            }

     37        }

     38
     39        public string Cars
     40        {
     41            get
     42            {
     43                return string.Join("", _person.Cars.ToArray());
     44            }

     45        }

     46
     47        [DebuggerDisplay("{_person.Age >= 21 ? \"Old Enough\"" + " : \" Too Young\"}", Type = "Alcohol Age Check")]
     48        public string AlcoholAgeCheck
     49        {
     50            get
     51            {
     52                return string.Empty;
     53            }

     54        }

     55
     56        private string ValidateObject()
     57        {
     58            List<string> violations = new List<string>();
     59
     60            // make sure that a first name has been specified
     61            if (0 == _person.FirstName.Length)
     62            {
     63                violations.Add("FirstName is empty");
     64            }

     65
     66            // make sure that a last name has been specified
     67            if (0 == _person.LastName.Length)
     68            {
     69                violations.Add("LastName is empty");
     70            }

     71
     72            // make sure that age is greater than 0
     73            if (0 > _person.Age)
     74            {
     75                violations.Add("Age is less than 0");
     76            }

     77
     78            // put additional validations here
     79            // return results to DebuggerDisplay attribute
     80            return string.Join("", violations.ToArray());
     81        }

     82
     83        [DebuggerDisplay("{ValidateObject()}")]
     84        public string BusinessRulesViolations
     85        {
     86            get
     87            {
     88                return string.Empty;
     89            }

     90        }

     91
     92        public string Person
     93        {
     94            get
     95            {
     96                return "Name = " + _person.FirstName + " " + _person.LastName + ", Age = " + _person.Age;
     97            }

     98        }

     99    }

    100}

    101
  • 相关阅读:
    软件设计模式之简单工厂模式
    CentOS中端口操作命令
    webapi core封装树操作出现错误
    asp.net mvc中调度器(Quartz)的使用
    软件设计模式之单例模式
    layer关闭弹窗(多种关闭弹窗方法)
    Hadoop大作业
    hive基本操作与应用
    用Python编写WordCount程序任务
    熟悉HBase基本操作
  • 原文地址:https://www.cnblogs.com/adam/p/659421.html
Copyright © 2011-2022 走看看