C#:IFormattable,和的IFormatProvider ICustomFormatter,当之间的连接使用什么 [英] C#: Connection between IFormattable, IFormatProvider and ICustomFormatter, and when to use what
问题描述
什么是 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 Framework中被使用,但是当我将执行这些自己,在这种情况下哪些类通常会实现什么样的接口以及如何做正确。
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
,即XXX
在{0:XXX}
。的String.Format
将如果对象支持该接口委托给一个对象的IFormattable.ToString
方法。
IFormattable
is an object which supports formats instring.Format
, i.e. thexxx
in{0:xxx}
.string.Format
will delegate to an object'sIFormattable.ToString
method if the object supports the interface.的IFormatProvider
是配置信息的来源,格式化使用的东西像区域性特定日期和货币的布局。IFormatProvider
is 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
检查供应商的ICustomFormatter
之前将其委托给对象的IFormattable.Format
,这反过来又可能要求的IFormatProvider
像的CultureInfo
区域性特定的数据。However, for situations like e.g.
DateTime
, where the instance you want to format already implementsIFormattable
yet you don't control the implementation (DateTime
is supplied in the BCL, you can't replace it easily), there is a mechanism to preventstring.Format
from simply usingIFormattable.ToString
. Instead, you implementIFormatProvider
, and when asked for anICustomFormatter
implementation, return one.string.Format
checks the provider for anICustomFormatter
before it delegates to the object'sIFormattable.Format
, which would in turn likely ask theIFormatProvider
for culture-specific data likeCultureInfo
.下面是一个程序,它显示了
的String.Format
问的IFormatProvider
对,和控制流如何去:Here is a program which shows what
string.Format
asks theIFormatProvider
for, 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())); } }
它打印这样的:
It prints this:
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); } }
运行时:
When run:
Asked for System.ICustomFormatter (format was "foobar")
这篇关于C#:IFormattable,和的IFormatProvider ICustomFormatter,当之间的连接使用什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!