C# 为类型输出格式化字符串

有两种方法可以为类型提供格式化字符串输出:

1、让类型继承IFormattable接口,实现IFormattable接口的ToString方法,需要开发者可以预见类型在格式化方面的要求。

调用输出未重写Tostring方法:

1     Person person = new Person
2     {
3         ID = "NO1",
4         FirstName = "You",
5         LastName = "Xu"
6     };
7     Console.WriteLine(person);

输出的是类型名称:

  改善1.Person 

为Person类实现IFormattable接口,重写ToString方法:

 1     class Person : IFormattable
 2     {
 3         public string ID { get; set; }
 4         public string FirstName { get; set; }
 5         public string LastName { get; set; }
 6 
 7         //实现IFormattable接口的ToString方法
 8         public string ToString(string format, IFormatProvider formatProvider)
 9         {
10             switch (format)
11             {
12                 case "Ch":
13                     return this.ToString();
14                 case "Eg":
15                     return string.Format("{0} {1}", FirstName, LastName);
16                 default:
17                     return this.ToString();
18             }
19         }
20 
21         //重写Object.ToString()
22         public override string ToString()
23         {
24             return string.Format("{0} {1}", LastName, FirstName);
25         }
26     }

调用输出修改后的代码:

1      Person person = new Person
2      {
3          ID = "NO1",
4          FirstName = "You",
5          LastName = "Xu"
6      };
7      Console.WriteLine(person);
8      Console.WriteLine(person.ToString("Ch", null));
9      Console.WriteLine(person.ToString("Eg", null));

输出结果:

Xu You
Xu You
You Xu

 

2、为类型自定义格式化器,可以根据需求的变化提供多个格式化器,随时增加或者修改它。

    class Person 
    {
        public string ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    //Person类的格式化器
    class PersonFormatter : IFormatProvider, ICustomFormatter
    {
        //实现IFprmatProvider接口
        public object GetFormat(Type formatType)
        {
            if (formatType == typeof(ICustomFormatter))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //实现ICustomFormatter接口
        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            Person person = arg as Person;
            if (person == null)
            {
                return string.Empty;
            }
            switch (format)
            {
                case "Ch":
                    return string.Format("{0} {1}", person.LastName, person.FirstName);
                case "Eg":
                    return string.Format("{0} {1}", person.FirstName, person.LastName);
                case "ChM":
                    return string.Format("{0} {1}:{2}", person.LastName, person.FirstName, person.ID);
                default:
                    return string.Format("{0} {1}", person.LastName, person.FirstName);
            }
        }
    }

调用格式化器代码:

 1      Person person = new Person
 2      {
 3          ID = "NO1",
 4          FirstName = "You",
 5          LastName = "Xu"
 6      };
 7      Console.WriteLine(person);
 8 
 9      PersonFormatter pFormatter = new PersonFormatter();
10 
11      Console.WriteLine(pFormatter.Format("Ch", person,null));
12      Console.WriteLine(pFormatter.Format("Eg", person,null));
13      Console.WriteLine(pFormatter.Format("ChM", person, null));

输出结果,因为没有重写Object.ToString方法,类型会输出类型名称:

改善1.Person
Xu You
You Xu
Xu You:NO1

 

在对IFormattable接口的ToString方法实现时,可以结合格式化器,让格式化输出在语法上支持更多的调用方式,修改ToString方法里switch结构的default部分:

 1     class Person : IFormattable
 2     {
 3         public string ID { get; set; }
 4         public string FirstName { get; set; }
 5         public string LastName { get; set; }
 6 
 7         //实现IFormattable接口的ToString方法
 8         public string ToString(string format, IFormatProvider formatProvider)
 9         {
10             switch (format)
11             {
12                 case "Ch":
13                     return this.ToString();
14                 case "Eg":
15                     return string.Format("{0} {1}", FirstName, LastName);
16                 default:
17                     ICustomFormatter customFormatter = formatProvider as ICustomFormatter;
18                     if (customFormatter == null)
19                     {
20                         return this.ToString();
21                     }
22                     return customFormatter.Format(format, this, null);
23             }
24         }
25 
26         //重写Object.ToString()
27         public override string ToString()
28         {
29             return string.Format("{0} {1}", LastName, FirstName);
30         }
31     }

调用代码:

 1     Person person = new Person()
 2     {
 3         ID = "NO1",
 4         FirstName = "You",
 5         LastName = "Xu"
 6     };
 7     Console.WriteLine(person);
 8 
 9     PersonFormatter pFormatter = new PersonFormatter();
10 
11     Console.WriteLine(pFormatter.Format("Ch", person, null));
12     Console.WriteLine(pFormatter.Format("Eg", person, null));
13     Console.WriteLine(pFormatter.Format("ChM", person, null));
14 
15     Console.WriteLine(person.ToString("Ch", pFormatter));
16     Console.WriteLine(person.ToString("Eg", pFormatter));
17     Console.WriteLine(person.ToString("ChM", pFormatter));

 

参考:《编写高质量代码改善C#程序的157个建议》陆敏技

你可能感兴趣的:(C# 为类型输出格式化字符串)