简体   繁体   English

ICustomFormatter中的IFormatProvider

[英]IFormatProvider In ICustomFormatter

We're looking to implement custom formatters, as per usual, best to use interfaces from Microsoft, so I find ICustomFormatter. 我们希望像往常一样实现自定义格式器,最好使用Microsoft的接口,因此我找到了ICustomFormatter。

http://msdn.microsoft.com/en-us/library/system.icustomformatter.format(v=vs.110).aspx http://msdn.microsoft.com/zh-CN/library/system.icustomformatter.format(v=vs.110).aspx

However the signature for the interface provides this: 但是,接口的签名提供了以下功能:

public string Format(string format, object arg, IFormatProvider formatProvider)
{

}

What would I even use formatProvider for? 我什至可以使用formatProvider? I've yet to see good examples of why you're concerned about what formatProvider has loaded this ICustomFormatter. 我还没有看到很好的例子说明为什么您担心哪个formatProvider加载了此ICustomFormatter。

The only example I've found is something like this: 我发现的唯一示例是这样的:

http://www.codeproject.com/Articles/6533/Custom-String-Formatting-in-NET http://www.codeproject.com/Articles/6533/Custom-String-Formatting-in-NET

if (formatProvider is NumberFormatInfo)
    if (((NumberFormatInfo)formatProvider).UseDiacritic)
        c = diacritic;

But this just looks like awful design (take an interface and then strongly coupling it). 但这看起来就像是糟糕的设计(采用接口然后将其强烈耦合)。

Am I missing some special use-case? 我是否缺少一些特殊用例?

The IFormatProvider is there to provide culture-specific and global formatting information to your custom formatter. IFormatProvider可以在其中为您的自定义格式化程序提供特定于区域性的全局格式化信息。 Of course, depending on what your custom formatter is doing, you might not need any of this information, but if you were formatting currency or floating point numbers or dates, for example, then you might well want it. 当然,取决于自定义格式化程序的工作,您可能不需要任何此信息,但是例如,如果要格式化货币或浮点数或日期,则可能需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM