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
What does IFormatProvider do?
提问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 CultureInfo
implements 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 tehvan
回答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 IFormatProvider
is 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 IFormatProvider
interface is normally implemented for you by a CultureInfo
class, 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 IFormatProvider
can be queried for are:
该接口是函数从文化中获取一组特定于文化的数据的网关。IFormatProvider
可以查询的两个常用区域性对象是:
DateTimeFormatInfo
NumberFormatInfo
DateTimeFormatInfo
NumberFormatInfo
The way it would normally work is you ask the IFormatProvider
to give you a DateTimeFormatInfo
object:
它通常工作的方式是你要求IFormatProvider
给你一个DateTimeFormatInfo
对象:
DateTimeFormatInfo format;
format = (DateTimeFormatInfo)provider.GetFormat(typeof(DateTimeFormatInfo));
if (format != null)
DoStuffWithDatesOrTimes(format);
There's also inside knowledge that any IFormatProvider
interface 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 DateTimeFormatInfo
from an IFormatProvider
:
要从DateTimeFormatInfo
an获取 aIFormatProvider
:
DateTimeFormatInfo format = DateTimeFormatInfo.GetInstance(provider);
To get a NumberFormatInfo
from an IFormatProvider
:
要从NumberFormatInfo
an获取 aIFormatProvider
:
NumberFormatInfo format = NumberFormatInfo.GetInstance(provider);
The value of IFormatProvider
is 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 IFormatProvider
for 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 LordsNameFormatInfo
class ride along inside an IFormatProvider
, and you can preserve the idiom.
这可以让您的自定义LordsNameFormatInfo
类在 内运行IFormatProvider
,并且您可以保留习语。
In reality you will never need to call GetFormat
method of IFormatProvider
yourself.
实际上,您永远不需要调用自己的GetFormat
方法IFormatProvider
。
Whenever you need an IFormatProvider
you can pass a CultureInfo
object:
每当您需要时,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 实现,以提供用于格式化或解析数字以及日期和时间值的特定于区域性的信息:
- The
NumberFormatInfo
class, 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 aNumberFormatInfo
object and used in numeric formatting operations, see Standard Numeric Format Strings and Custom Numeric Format Strings. - The
DateTimeFormatInfo
class, 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 aDateTimeFormatInfo
object and used in numeric formatting operations, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. - The
CultureInfo
class, which represents a particular culture. ItsGetFormat
method returns a culture-specificNumberFormatInfo
orDateTimeFormatInfo
object, depending on whether theCultureInfo
object is used in a formatting or parsing operation that involves numbers or dates and times.
- 的
NumberFormatInfo
类,它提供一个用于格式化数字,如货币,千位分隔符和小数分隔符号用于特定文化的信息。有关NumberFormatInfo
对象识别并在数字格式化操作中使用的预定义格式字符串的信息,请参阅标准数字格式字符串和自定义数字格式字符串。 - 的
DateTimeFormatInfo
类,它提供了用于日期和时间格式,如特定的文化或日期的年,月,日组成部分的顺序和格式的日期和时间分隔符符号的信息。有关DateTimeFormatInfo
对象识别并在数字格式化操作中使用的预定义格式字符串的信息,请参阅标准日期和时间格式字符串和自定义日期和时间格式字符串。 - 的
CultureInfo
类,它代表一个特定的文化。它的GetFormat
方法返回特定于区域性的对象NumberFormatInfo
或DateTimeFormatInfo
对象,具体取决于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[])
.