Tip13 爲類型輸出格式化字符串
有兩種方法爲類型提供格式化的字符串輸出:
1、讓類型繼承接口IFormattable。(需要預見類型在格式化方面的需求)
2、讓類型繼承接口IFormatProvider和ICustomFormatter,爲類型自定義格式化器。
方法1的例子:
class Person : IFormattable
{
public string IDCode { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string ToString(string format, IFormatProvider formatProvider)
{
switch (format)
{
case "Ch":
return this.ToString();
case "Eg":
return string.Format("{0} {1}", FirstName, LastName);
default:
return this.ToString();
}
}
public override string ToString()
{
return string.Format("{0} {1}", LastName, FirstName);
}
}
Person person = new Person() { FirstName = "Jessica",
LastName = "Hu", IDCode = "NB123" };
Console.WriteLine(person);
Console.WriteLine(person.ToString("Ch", null));
Console.WriteLine(person.ToString("Eg", null));
//輸出如下:
Hu Jessica
Hu Jessica
Jessica Hu
方法2的例子:
class Person
{
public string IDCode { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
class PersonFormatter : IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
{
return this;
}
else
{
return null;
}
}
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}", person.LastName, person.FirstName, person.IDCode);
default:
return string.Format("{0} {1}", person.FirstName, person.LastName);
}
}
Person person = new Person() { FirstName = "Jessica", LastName = "Hu", IDCode = "NB123" };
Console.WriteLine(person.ToString());
PersonFormatter pFormatter = new PersonFormatter();
Console.WriteLine(pFormatter.Format("Ch", person,null));
Console.WriteLine(pFormatter.Format("Eg", person, null));
Console.WriteLine(pFormatter.Format("ChM",person,null));
//輸出:
ConsoleApplication1.Person
Hu Jessica
Jessica Hu
Hu Jessica
其實可以在方法1的ToString方法稍作修改,讓格式化輸出在語法上支持更多的調用方式:
class Person : IFormattable
{
public string IDCode { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
//實現接口IFormattable的方法ToString
public string ToString(string format, IFormatProvider formatProvider)
{
switch (format)
{
case "Ch":
return this.ToString();
case "Eg":
return string.Format("{0} {1}", FirstName, LastName);
default:
//return this.ToString();
ICustomFormatter customFormatter = formatProvider as ICustomFormatter;
if (customFormatter == null)
{
return this.ToString();
}
return customFormatter.Format(format, this, null);
}
}
//重寫Object.ToString()
public override string ToString()
{
return string.Format("{0} {1}", LastName, FirstName);
}
}
調用如下:
Person person = new Person() { FirstName = "Jessica", LastName = "Hu", IDCode = "NB123" };
Console.WriteLine(person.ToString());
PersonFomatter pFormatter = new PersonFomatter();
//第一類格式化輸出語法
Console.WriteLine(pFormatter.Format("Ch", person, null));
Console.WriteLine(pFormatter.Format("Eg", person, null));
Console.WriteLine(pFormatter.Format("ChM", person, null));
//第二類格式化輸出語法,也更簡潔
Console.WriteLine(person.ToString("Ch", pFormatter));
Console.WriteLine(person.ToString("Eg", pFormatter));
Console.WriteLine(person.ToString("ChM", pFormatter));