在WinGrid (如果您必须知道的话)中,我得到了一个包含ints的列,它的值是几秒钟,您可以从中计算出一个时间。我创建了一个IFormatProvider/ICustomFormatter,它就是这样做的。在网格初始化期间,我设置了格式和FormatInfo参数。
但是,在我的自定义类型格式化程序上调用GetFormat时,类型参数总是一个NumberFormatInfo,而不是一个ICustomFormatter。为什么?
这是我的课,以防有帮助:
public class SecToTime : IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
{
return this;
}
else
{
return null;
}
}
public string Format(string format, object arg, IFormatProvider provider)
{
if (arg is int)
{
int seconds = (int)arg;
int hours = (int)Math.Truncate((double)seconds / 3600);
int minutes = (int)Math.Truncate((double)(seconds / 60) % 60);
seconds = seconds % 60;
return string.Format("{0:hh:mm:ss}", new DateTime(0, 0, 0, hours, minutes, seconds));
}
else
throw new ArgumentNullException();
}
}发布于 2011-03-09 20:43:00
引用(伟大的) Mike的话说:
除非还设置了Format属性,否则不会调用FormatInfo。这是我能想到为什么不叫它的唯一原因。
来源:这个不脆弱性论坛的帖子
要测试它,尝试将列Format属性设置为.:)
https://stackoverflow.com/questions/5251502
复制相似问题