如何为 DateTime 创建和使用自定义 IFormatProvider?
我试图创建一个 IFormatProvider 实现来识别 DateTime 对象的自定义格式字符串。这是我的实现:
public class MyDateFormatProvider : IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
{
return this;
}
return null;
}
public string Format(string format, object arg, IFormatProvider formatProvider)
{
if(arg == null) throw new ArgumentNullException("arg");
if (arg.GetType() != typeof(DateTime)) return arg.ToString();
DateTime date = (DateTime)arg;
switch(format)
{
case "mycustomformat":
switch(CultureInfo.CurrentCulture.Name)
{
case "en-GB":
return date.ToString("ddd dd MMM");
default:
return date.ToString("ddd MMM dd");
}
default:
throw new FormatException();
}
}
我希望能够在 DateTime.ToString(string format, IFormatProviderprovider)
方法中使用它,如下所示,但是:
DateTime d = new DateTime(2000, 1, 2);
string s = d.ToString("mycustomformat", new MyDateFormatProvider());
在该示例中,在美国文化中运行,结果是“00cu0Ao00or0aA”,显然是因为正在解释标准日期时间格式字符串。
但是,当我按以下方式使用同一类时:
DateTime d = new DateTime(2000, 1, 2);
string s = String.Format(new MyDateFormatProvider(), "{0:mycustomformat}", d);
我得到了我所期望的,即 "Sun Jan 02"
我不明白不同的结果。有人可以解释一下吗?
谢谢!
I was trying to create an IFormatProvider
implementation that would recognize custom format strings for DateTime objects. Here is my implementation:
public class MyDateFormatProvider : IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
{
return this;
}
return null;
}
public string Format(string format, object arg, IFormatProvider formatProvider)
{
if(arg == null) throw new ArgumentNullException("arg");
if (arg.GetType() != typeof(DateTime)) return arg.ToString();
DateTime date = (DateTime)arg;
switch(format)
{
case "mycustomformat":
switch(CultureInfo.CurrentCulture.Name)
{
case "en-GB":
return date.ToString("ddd dd MMM");
default:
return date.ToString("ddd MMM dd");
}
default:
throw new FormatException();
}
}
I was expecting to be able to use it in the DateTime.ToString(string format, IFormatProvider provider)
method like so, but :
DateTime d = new DateTime(2000, 1, 2);
string s = d.ToString("mycustomformat", new MyDateFormatProvider());
In that example, running in the US Culture, the result is "00cu0Ao00or0aA"
, apparently because the standard DateTime format strings are being interpreted.
However, when I use the same class in the following way:
DateTime d = new DateTime(2000, 1, 2);
string s = String.Format(new MyDateFormatProvider(), "{0:mycustomformat}", d);
I get what I expect, namely "Sun Jan 02"
I don't understand the different results. Could someone explain?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 Reflector 检查
DateTime.ToString
方法显示DateTime
结构使用DateTimeFormatInfo.GetInstance
方法来获取用于格式化的提供程序。DateTimeFormatInfo.GetInstance
从传入的提供程序请求DateTimeFormatInfo
类型的格式化程序,从不用于ICustomFormmater
,因此它仅返回DateTimeFormatInfo
的实例。 code>DateTimeFormatInfo 或CultureInfo
(如果未找到提供者)。似乎DateTime.ToString
方法不像StringBuilder.Format
方法那样遵循ICustomFormatter
接口,正如您的String.格式
示例所示。我同意
DateTime.ToString
方法应该支持ICustomFormatter
接口,但目前似乎不支持。在 .NET 4.0 中,这一切可能已经改变或将会改变。Checking the
DateTime.ToString
method with Reflector shows that theDateTime
structure uses theDateTimeFormatInfo.GetInstance
method to get the provider to be used for formatting. TheDateTimeFormatInfo.GetInstance
requests a formatter of typeDateTimeFormatInfo
from the provider passed in, never forICustomFormmater
, so it only returns an instance of aDateTimeFormatInfo
orCultureInfo
if no provider is found. It seems that theDateTime.ToString
method does not honor theICustomFormatter
interface like theStringBuilder.Format
method does, as yourString.Format
example shows.I agree that the
DateTime.ToString
method should support theICustomFormatter
interface, but it does not seem to currently. This may all have changed or will change in .NET 4.0.使用扩展方法:)
Use extension method :)
简而言之,虽然
允许您传递任何实现
IFormatProvider
作为其参数之一,但它实际上只支持在其代码中实现IFormatProvider
的 2 种可能类型:DateTimeFormatInfo< /code> 或
CultureInfo
如果您的参数无法转换(使用
as
)为其中之一,则该方法将默认为CurrentCulture
。String.Format
不受此类限制。The short explanation is that while
lets you pass anything implementing
IFormatProvider
as one of its parameters, it actually only supports 2 possible types implementingIFormatProvider
inside its code:DateTimeFormatInfo
orCultureInfo
If your parameter cannot be casted (using
as
) as either or those, the method will default toCurrentCulture
.String.Format
is not limited by such bounds.