Ray's playground

 

Item 5: Always Provide ToString()(Effective C#)

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 namespace EffectiveCSharpItem5
  7 {
  8     public class Customer : IFormattable
  9     {
 10         public string Name
 11         {
 12             get;
 13             set;
 14         }
 15 
 16         public decimal Revenue
 17         {
 18             get;
 19             set;
 20         }
 21 
 22         public string ContactPhone
 23         {
 24             get;
 25             set;
 26         }
 27 
 28         public override string ToString()
 29         {
 30             return Name;
 31         }
 32 
 33         string System.IFormattable.ToString(string format, IFormatProvider formatProvider)
 34         {
 35             if (formatProvider != null)
 36             {
 37                 ICustomFormatter fmt = formatProvider.GetFormat(this.GetType()) as ICustomFormatter;
 38                 if (fmt != null)
 39                 {
 40                     return fmt.Format(format, this, formatProvider);
 41                 }
 42             }
 43 
 44             switch (format)
 45             {
 46                 case "r":
 47                     return Revenue.ToString();
 48                 case "p":
 49                     return ContactPhone;
 50                 case "nr":
 51                     return string.Format("{0,20}, {1,10:C}", Name, Revenue);
 52                 case "np":
 53                     return string.Format("{0,20}, {1,15}", Name, ContactPhone);
 54                 case "pr":
 55                     return string.Format("{0,15}, {1,10:C}", ContactPhone, Revenue);
 56                 case "pn":
 57                     return string.Format("{0,15}, {1,20}", ContactPhone, Name);
 58                 case "rn":
 59                     return string.Format("{0,10:C}, {1,20}", Revenue, Name);
 60                 case "rp":
 61                     return string.Format("{0,10:C}, {1,20}", Revenue, ContactPhone);
 62                 case "nrp":
 63                     return string.Format("{0,20}, {1,10:C}, {2,15}", Name, Revenue, ContactPhone);
 64                 case "npr":
 65                     return string.Format("{0,20}, {1,15}, {2,10:C}", Name, ContactPhone, Revenue);
 66                 case "pnr":
 67                     return string.Format("{0,15}, {1,20}, {2,10:C}", ContactPhone, Name, Revenue);
 68                 case "prn":
 69                     return string.Format("{0,15}, {1,10:C}, {2,15}", ContactPhone, Revenue, Name);
 70                 case "rpn":
 71                     return string.Format("{0,10:C}, {1,15}, {2,20}", Revenue, ContactPhone, Name);
 72                 case "rnp":
 73                     return string.Format("{0,10:C}, {1,20}, {2,15}", Revenue, Name, ContactPhone);
 74                 case "n":
 75                 case "G":
 76                 default:
 77                     return Name;
 78             }
 79         }
 80     }
 81 
 82     public class CustomFormatter : IFormatProvider, ICustomFormatter
 83     {
 84         #region IFormatProvider Members
 85 
 86         public object GetFormat(Type formatType)
 87         {
 88             if (formatType == typeof(ICustomFormatter))
 89             {
 90                 return this;
 91             }
 92             return null;
 93         }
 94 
 95         #endregion
 96         
 97         #region ICustomFormatter Members
 98 
 99         public string Format(string format, object arg, IFormatProvider formatProvider)
100         {
101             Customer c = arg as Customer;
102             if (c == null)
103             {
104                 return arg.ToString();
105             }
106 
107             return string.Format("{0,50}, {1,15}, {2,10:C}", c.Name, c.ContactPhone, c.Revenue);
108         }
109 
110         #endregion
111     }
112 
113     class Program
114     {
115         static void Main(string[] args)
116         {
117             IFormattable c1 = new Customer() { Name="Ray", ContactPhone="12345", Revenue=200000 };
118             
119             Console.WriteLine("Customer record: {0}", c1.ToString("nrp"null));
120             Console.WriteLine(string.Format(new CustomFormatter(), "{0}", c1));
121             Console.Read();
122         }
123     }
124 }

 

posted on 2011-01-11 22:37  Ray Z  阅读(178)  评论(0)    收藏  举报

导航