C#: Connection between IFormattable, IFormatProvider and ICustomFormatter, and when to use what(C#:IFormattable、IFormatProvider 和 ICustomFormatter 之间的连接,以及何时使用什么)
问题描述
IFormattable、IFormatProvider和ICustomFormatter有什么区别和联系,什么时候用?一个简单的实现示例也会非常好.
What are the difference and connection between IFormattable, IFormatProvider and ICustomFormatter and when would they be used? A simple implementation example would be very nice too.
我并不是说什么时候在 .net 框架中使用它,而是什么时候我自己实现这些,在这种情况下,什么类通常会实现什么接口以及如何正确地实现它.
And I don't really mean when it is used in the .net framework, but when I would implement these myself and in that case what classes would typically implement what interface and how to do it properly.
推荐答案
IFormattable是一个支持string.Format格式的对象,即{0:xxx中的xxx}代码>.如果对象支持接口,string.Format将委托给对象的IFormattable.ToString方法.IFormattableis an object which supports formats instring.Format, i.e. thexxxin{0:xxx}.string.Formatwill delegate to an object'sIFormattable.ToStringmethod if the object supports the interface.IFormatProvider是格式化程序用于特定文化的日期和货币布局等配置信息的来源.IFormatProvideris a source of configuration information that formatters use for things like culture-specific date and currency layout.但是,对于例如
DateTime,您要格式化的实例已经实现IFormattable但您不控制实现(DateTime在 BCL 中提供,您不能轻易替换),有一种机制可以防止string.Format简单地使用IFormattable.ToString.相反,您实现IFormatProvider,当被要求提供ICustomFormatter实现时,返回一个.string.Format在委托给对象的IFormattable.Format之前检查提供程序的ICustomFormatter,这反过来可能会询问IFormatProvider用于特定于文化的数据,例如CultureInfo.However, for situations like e.g.
DateTime, where the instance you want to format already implementsIFormattableyet you don't control the implementation (DateTimeis supplied in the BCL, you can't replace it easily), there is a mechanism to preventstring.Formatfrom simply usingIFormattable.ToString. Instead, you implementIFormatProvider, and when asked for anICustomFormatterimplementation, return one.string.Formatchecks the provider for anICustomFormatterbefore it delegates to the object'sIFormattable.Format, which would in turn likely ask theIFormatProviderfor culture-specific data likeCultureInfo.这是一个程序,它显示了
string.Format向IFormatProvider请求什么,以及控制流程是如何进行的:Here is a program which shows what
string.Formatasks theIFormatProviderfor, and how the flow of control goes:using System; using System.Globalization; class MyCustomObject : IFormattable { public string ToString(string format, IFormatProvider provider) { Console.WriteLine("ToString("{0}", provider) called", format); return "arbitrary value"; } } class MyFormatProvider : IFormatProvider { public object GetFormat(Type formatType) { Console.WriteLine("Asked for {0}", formatType); return CultureInfo.CurrentCulture.GetFormat(formatType); } } class App { static void Main() { Console.WriteLine( string.Format(new MyFormatProvider(), "{0:foobar}", new MyCustomObject())); } }它打印这个:
Asked for System.ICustomFormatter ToString("foobar", provider) called arbitrary value如果格式提供程序更改为返回自定义格式化程序,它将接管:
If the format provider is changed to return a custom formatter, it takes over:
class MyFormatProvider : IFormatProvider { public object GetFormat(Type formatType) { Console.WriteLine("Asked for {0}", formatType); if (formatType == typeof(ICustomFormatter)) return new MyCustomFormatter(); return CultureInfo.CurrentCulture.GetFormat(formatType); } } class MyCustomFormatter : ICustomFormatter { public string Format(string format, object arg, IFormatProvider provider) { return string.Format("(format was "{0}")", format); } }运行时:
Asked for System.ICustomFormatter (format was "foobar")这篇关于C#:IFormattable、IFormatProvider 和 ICustomFormatter 之间的连接,以及何时使用什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C#:IFormattable、IFormatProvider 和 ICustomFormatter 之间的连接,以及何时使用什么
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 使用 rss + c# 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
