C# IFormatProvider 有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/506676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:05:32  来源:igfitidea点击:

What does IFormatProvider do?

c#asp.netiformatprovider

提问by Moulde

I was playing around with the Datetime.ParseExact method, and it wants an IFormatProvider...

我在玩 Datetime.ParseExact 方法,它想要一个 IFormatProvider ......

It works inputting null, but what exactly does it do?

它可以输入空值,但它到底是做什么的?

采纳答案by Andrei R?nea

In adition to Ian Boyd's answer:

除了伊恩博伊德的回答:

Also CultureInfoimplements this interface and can be used in your case. So you could parse a French date string for example; you could use

CultureInfo实现了这个接口,可以在你的情况下使用。例如,您可以解析法语日期字符串;你可以用

var ci = new CultureInfo("fr-FR");
DateTime dt = DateTime.ParseExact(yourDateInputString, yourFormatString, ci);

回答by Aamir

You can see here http://msdn.microsoft.com/en-us/library/system.iformatprovider.aspx

你可以在这里看到http://msdn.microsoft.com/en-us/library/system.iformatprovider.aspx

See the remarks and example section there.

请参阅那里的备注和示例部分。

回答by Gerrie Schenck

The DateTimeFormatInfoclass implements this interface, so it allows you to control the formatting of your DateTime strings.

的DateTimeFormatInfo类实现这个接口,所以它可以让您控制您的日期时间字符串的格式。

回答by Raithlin

IFormatProviderprovides culture info to the method in question. DateTimeFormatInfoimplements IFormatProvider, and allows you to specify the format you want your date/time to be displayed in. Examples can be found on the relevant MSDN pages.

IFormatProvider为相关方法提供区域性信息。DateTimeFormatInfo实现了 IFormatProvider,并允许您指定您希望日期/时间显示的格式。可以在相关 MSDN 页面上找到示例。

回答by Tim

Passing null as the IFormatProvideris not the correct way to do this. If the user has a custom date/time format on their PC you'll have issues in parsing and converting to string. I've just fixed a bug where somebody had passed null as the IFormatProvider when converting to string.

传递 null 作为IFormatProvider不是执行此操作的正确方法。如果用户在他们的 PC 上有自定义的日期/时间格式,您将在解析和转换为字符串时遇到问题。我刚刚修复了一个错误,即有人在转换为字符串时将 null 作为 IFormatProvider 传递。

Instead you should be using CultureInfo.InvariantCulture

相反,你应该使用 CultureInfo.InvariantCulture

Wrong:

错误的:

string output = theDate.ToString("dd/MM/yy HH:mm:ss.fff", null);

Correct:

正确的:

string output = theDate.ToString("dd/MM/yy HH:mm:ss.fff", CultureInfo.InvariantCulture);

回答by Ian Boyd

The IFormatProviderinterface is normally implemented for you by a CultureInfoclass, e.g.:

IFormatProvider接口通常由一个CultureInfo类为您实现,例如:

  • CultureInfo.CurrentCulture
  • CultureInfo.CurrentUICulture
  • CultureInfo.InvariantCulture
  • CultureInfo.CreateSpecificCulture("de-CA") //German (Canada)
  • CultureInfo.CurrentCulture
  • CultureInfo.CurrentUICulture
  • CultureInfo.InvariantCulture
  • CultureInfo.CreateSpecificCulture("de-CA") //German (Canada)

The interface is a gateway for a function to get a set of culture-specific data from a culture. The two commonly available culture objects that an IFormatProvidercan be queried for are:

该接口是函数从文化中获取一组特定于文化的数据的网关。IFormatProvider可以查询的两个常用区域性对象是:

  • DateTimeFormatInfo
  • NumberFormatInfo
  • DateTimeFormatInfo
  • NumberFormatInfo

The way it would normally work is you ask the IFormatProviderto give you a DateTimeFormatInfoobject:

它通常工作的方式是你要求IFormatProvider给你一个DateTimeFormatInfo对象:

DateTimeFormatInfo format;
format = (DateTimeFormatInfo)provider.GetFormat(typeof(DateTimeFormatInfo));
if (format != null)
   DoStuffWithDatesOrTimes(format);

There's also inside knowledge that any IFormatProviderinterface is likely being implemented by a class that descends from CultureInfo, or descends from DateTimeFormatInfo, so you could cast the interface directly:

还有内部知识,任何IFormatProvider接口都可能由继承自CultureInfo或继承自的类实现DateTimeFormatInfo,因此您可以直接转换接口:

CultureInfo info = provider as CultureInfo;
if (info != null)
   format = info.DateTimeInfo;
else
{
   DateTimeFormatInfo dtfi = provider as DateTimeFormatInfo;
   if (dtfi != null)
       format = dtfi;
   else
       format = (DateTimeFormatInfo)provider.GetFormat(typeof(DateTimeFormatInfo));
}

if (format != null)
   DoStuffWithDatesOrTimes(format);

But don't do that

但不要那样做

All that hard work has already been written for you:

所有辛苦的工作都已经为你写好了:

To get a DateTimeFormatInfofrom an IFormatProvider:

要从DateTimeFormatInfoan获取 aIFormatProvider

DateTimeFormatInfo format = DateTimeFormatInfo.GetInstance(provider);

To get a NumberFormatInfofrom an IFormatProvider:

要从NumberFormatInfoan获取 aIFormatProvider

NumberFormatInfo format = NumberFormatInfo.GetInstance(provider);

The value of IFormatProvideris that you create your own culture objects. As long as they implement IFormatProvider, and return objects they're asked for, you can bypass the built-in cultures.

的价值IFormatProvider在于您可以创建自己的文化对象。只要他们实现IFormatProvider,并返回他们要求的对象,您就可以绕过内置文化。

You can also use IFormatProviderfor a way of passing arbitrary culture objects - through the IFormatProvider. E.g. the name of god in different cultures

您还可以使用IFormatProvider传递任意文化对象的方式 - 通过IFormatProvider. 例如,不同文化中的上帝之名

  • god
  • God
  • Jehova
  • Yahwe
  • ????
  • ???? ??? ????
  • 上帝
  • 上帝
  • 耶和华
  • 耶和华
  • ???
  • ?????????

This lets your custom LordsNameFormatInfoclass ride along inside an IFormatProvider, and you can preserve the idiom.

这可以让您的自定义LordsNameFormatInfo类在 内运行IFormatProvider,并且您可以保留习语。

In reality you will never need to call GetFormatmethod of IFormatProvideryourself.

实际上,您永远不需要调用自己的GetFormat方法IFormatProvider

Whenever you need an IFormatProvideryou can pass a CultureInfoobject:

每当您需要时,IFormatProvider您都可以传递一个CultureInfo对象:

DateTime.Now.ToString(CultureInfo.CurrentCulture);

endTime.ToString(CultureInfo.InvariantCulture);

transactionID.toString(CultureInfo.CreateSpecificCulture("qps-ploc"));

Note: Any code is released into the public domain. No attribution required.

注意:任何代码都发布到公共领域。不需要归属。

回答by Bigeyes

By MSDN

通过MSDN

The .NET Framework includes the following threepredefined IFormatProvider implementations to provide culture-specific information that is used in formatting or parsing numeric and date and time values:

.NET Framework 包括以下三个预定义的 IFormatProvider 实现,以提供用于格式化或解析数字以及日期和时间值的特定于区域性的信息:

  1. The NumberFormatInfoclass, which provides information that is used to format numbers, such as the currency, thousands separator, and decimal separator symbols for a particular culture. For information about the predefined format strings recognized by a NumberFormatInfoobject and used in numeric formatting operations, see Standard Numeric Format Strings and Custom Numeric Format Strings.
  2. The DateTimeFormatInfoclass, which provides information that is used to format dates and times, such as the date and time separator symbols for a particular culture or the order and format of a date's year, month, and day components. For information about the predefined format strings recognized by a DateTimeFormatInfoobject and used in numeric formatting operations, see Standard Date and Time Format Strings and Custom Date and Time Format Strings.
  3. The CultureInfoclass, which represents a particular culture. Its GetFormatmethod returns a culture-specific NumberFormatInfoor DateTimeFormatInfoobject, depending on whether the CultureInfoobject is used in a formatting or parsing operation that involves numbers or dates and times.
  1. NumberFormatInfo类,它提供一个用于格式化数字,如货币,千位分隔符和小数分隔符号用于特定文化的信息。有关NumberFormatInfo对象识别并在数字格式化操作中使用的预定义格式字符串的信息,请参阅标准数字格式字符串和自定义数字格式字符串。
  2. DateTimeFormatInfo类,它提供了用于日期和时间格式,如特定的文化或日期的年,月,日组成部分的顺序和格式的日期和时间分隔符符号的信息。有关DateTimeFormatInfo对象识别并在数字格式化操作中使用的预定义格式字符串的信息,请参阅标准日期和时间格式字符串和自定义日期和时间格式字符串。
  3. CultureInfo类,它代表一个特定的文化。它的GetFormat方法返回特定于区域性的对象NumberFormatInfoDateTimeFormatInfo对象,具体取决于CultureInfo对象是否用于涉及数字或日期和时间的格式化或解析操作。

The .NET Framework also supports custom formatting. This typically involves the creation of a formatting class that implements both IFormatProvider and ICustomFormatter. An instance of this class is then passed as a parameter to a method that performs a custom formatting operation, such as String.Format(IFormatProvider,?String,?Object[]).

.NET Framework 还支持自定义格式。这通常涉及创建实现 IFormatProvider 和 ICustomFormatter 的格式设置类。然后将此类的实例作为参数传递给执行自定义格式设置操作的方法,例如String.Format(IFormatProvider,?String,?Object[]).