本文为原创文章、源代码为原创代码,如转载/复制,请在网页/代码处明显位置标明原文名称、作者及网址,谢谢!
开发工具:VS2017
语言:C#
DotNet版本:.Net FrameWork 4.0及以上
一、编写一个Person类,代码如下:
class Person
{
public string FirstName { set; get; }
public string LastName { set; get; }
}
并让Person类继承IFormattable,代码如下:
class Person:IFormattable
{
public string FirstName { set; get; }
public string LastName { set; get; }
public string ToString(string format, IFormatProvider formatProvider)
{
//关键代码,后面给出
}
}
这里将会列出需要实现IFormattable的方法ToString(string format, IFormatProvider formatProvider),这里是关键代码,用来格式字符串,暂时不给出,由后面给出。
二、编写 PersonFormatter类,让其继承IFormatProvider及ICustomFormatter,用于对字符串进行格式化,代码如下:
class PersonFormatter : IFormatProvider,ICustomFormatter
{
public string Format(string format, object arg, IFormatProvider formatProvider)
{
//Format实现代码
}
public object GetFormat(Type formatType)
{
//GetFormat实现代码
}
}
Format:用于格式化字符串
Format的实现代码如下:
Person person = arg as Person;
switch(format)
{
case "CH":return $"{person.LastName} {person.FirstName}";
case "EN":return $"{person.FirstName} {person.LastName}";
default: return $"{person.LastName} {person.FirstName}";
}
GetFormat的实现代码如下:
if (formatType == typeof(ICustomFormatter)) return this;
return null;
因此,PersonFormatter类的代码如下:
class PersonFormatter : IFormatProvider,ICustomFormatter
{
public string Format(string format, object arg, IFormatProvider formatProvider)
{
Person person = arg as Person;
switch(format)
{
case "CH":return $"{person.LastName} {person.FirstName}";
case "EN":return $"{person.FirstName} {person.LastName}";
default: return $"{person.LastName} {person.FirstName}";
}
}
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter)) return this;
return null;
}
}
三、实现Person类IFormattable接口ToString方法,代码如下:
ICustomFormatter customFormatter = formatProvider as ICustomFormatter;
if (customFormatter == null) return this.ToString();
return customFormatter.Format(format, this, null);
最终Person类代码如下:
class Person:IFormattable
{
public string FirstName { set; get; }
public string LastName { set; get; }
public string ToString(string format, IFormatProvider formatProvider)
{
ICustomFormatter customFormatter = formatProvider as ICustomFormatter;
if (customFormatter == null) return this.ToString();
return customFormatter.Format(format, this, null);
}
}
四、使用Peson类的ToString方法,编写以下代码:
Person p1 = new Person { FirstName = "XY", LastName = "CN" };
PersonFormatter pf = new PersonFormatter();
string s1 = p1.ToString("CN", pf);
Console.WriteLine(s1);
string s2 = p1.ToString("EN", pf);
Console.WriteLine(s2);
五、运行结果:
六、附上完整源码:
class Program
{
static void Main(string[] args)
{
Person p1 = new Person { FirstName = "XY", LastName = "CN" };
PersonFormatter pf = new PersonFormatter();
string s1 = p1.ToString("CN", pf);
Console.WriteLine(s1);
string s2 = p1.ToString("EN", pf);
Console.WriteLine(s2);
}
}
class PersonFormatter : IFormatProvider,ICustomFormatter
{
public string Format(string format, object arg, IFormatProvider formatProvider)
{
Person person = arg as Person;
switch(format)
{
case "CH":return $"{person.LastName} {person.FirstName}";
case "EN":return $"{person.FirstName} {person.LastName}";
default: return $"{person.LastName} {person.FirstName}";
}
}
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter)) return this;
return null;
}
}
class Person:IFormattable
{
public string FirstName { set; get; }
public string LastName { set; get; }
public string ToString(string format, IFormatProvider formatProvider)
{
ICustomFormatter customFormatter = formatProvider as ICustomFormatter;
if (customFormatter == null) return this.ToString();
return customFormatter.Format(format, this, null);
}
}