首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GetFormat从未获得IFormatProvider类型

GetFormat从未获得IFormatProvider类型
EN

Stack Overflow用户
提问于 2011-03-09 20:05:06
回答 1查看 735关注 0票数 0

在WinGrid (如果您必须知道的话)中,我得到了一个包含ints的列,它的值是几秒钟,您可以从中计算出一个时间。我创建了一个IFormatProvider/ICustomFormatter,它就是这样做的。在网格初始化期间,我设置了格式和FormatInfo参数。

但是,在我的自定义类型格式化程序上调用GetFormat时,类型参数总是一个NumberFormatInfo,而不是一个ICustomFormatter。为什么?

这是我的课,以防有帮助:

代码语言:javascript
复制
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();
    }
}
EN

回答 1

Stack Overflow用户

发布于 2011-03-09 20:43:00

引用(伟大的) Mike的话说:

除非还设置了Format属性,否则不会调用FormatInfo。这是我能想到为什么不叫它的唯一原因。

来源:这个不脆弱性论坛的帖子

要测试它,尝试将列Format属性设置为.:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5251502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档