本文为原创文章、源代码为原创代码,如转载/复制,请在网页/代码处明显位置标明原文名称、做者及网址,谢谢!ide
开发工具:VS2017工具
语言:C#开发工具
DotNet版本:.Net FrameWork 4.0及以上this
1、编写一个Person类,代码以下:spa
class Person { public string FirstName { set; get; } public string LastName { set; get; } }
并让Person类继承IFormattable,代码以下:code
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),这里是关键代码,用来格式字符串,暂时不给出,由后面给出。orm
2、编写 PersonFormatter类,让其继承IFormatProvider及ICustomFormatter,用于对字符串进行格式化,代码以下:blog
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; } }
3、实现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); } }
4、使用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);
5、运行结果:
6、附上完整源码:
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); } }