C#:IFormattable,和的IFormatProvider ICustomFormatter,当之间的连接使用什么 [英] C#: Connection between IFormattable, IFormatProvider and ICustomFormatter, and when to use what

查看:178
本文介绍了C#:IFormattable,和的IFormatProvider ICustomFormatter,当之间的连接使用什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是 IFormattable 的IFormatProvider 之间的区别与联系和 ICustomFormatter 时,他们会使用吗?一个简单的实现示例将是非常好的了。

What are the difference and connection between IFormattable, IFormatProvider and ICustomFormatter and when would they be used? A simple implementation example would be very nice too.

和我真的不意味着当它在.NET Framework中被使用,但是当我将执行这些自己,在这种情况下哪些类通常会实现什么样的接口以及如何做正确。

And I don't really mean when it is used in the .net framework, but when I would implement these myself and in that case what classes would typically implement what interface and how to do it properly.

推荐答案


  • IFormattable 是支持格式的String.Format ,即 XXX {0:XXX} 的String.Format 将如果对象支持该接口委托给一个对象的 IFormattable.ToString 方法。

  • IFormattable is an object which supports formats in string.Format, i.e. the xxx in {0:xxx}. string.Format will delegate to an object's IFormattable.ToString method if the object supports the interface.

    的IFormatProvider 是配置信息的来源,格式化使用的东西像区域性特定日期和货币的布局。

    IFormatProvider is a source of configuration information that formatters use for things like culture-specific date and currency layout.

    不过,对于像如情况的DateTime ,要格式化已经农具实例 IFormattable 但你不控制实现(的DateTime 在BCL提供,你不能取代它很容易),有防止的String.Format 从简单的机制使用 IFormattable.ToString 。相反,你实施的IFormatProvider ,并在需要时为 ICustomFormatter 实现,则返回之一。 的String.Format 检查供应商的 ICustomFormatter 之前将其委托给对象的 IFormattable.Format ,这反过来又可能要求的IFormatProvider 的CultureInfo 区域性特定的数据。

    However, for situations like e.g. DateTime, where the instance you want to format already implements IFormattable yet you don't control the implementation (DateTime is supplied in the BCL, you can't replace it easily), there is a mechanism to prevent string.Format from simply using IFormattable.ToString. Instead, you implement IFormatProvider, and when asked for an ICustomFormatter implementation, return one. string.Format checks the provider for an ICustomFormatter before it delegates to the object's IFormattable.Format, which would in turn likely ask the IFormatProvider for culture-specific data like CultureInfo.

    下面是一个程序,它显示了的String.Format 的IFormatProvider 对,和控制流如何去:

    Here is a program which shows what string.Format asks the IFormatProvider for, and how the flow of control goes:

    using System;
    using System.Globalization;
    
    class MyCustomObject : IFormattable
    {
        public string ToString(string format, IFormatProvider provider)
        {
            Console.WriteLine("ToString(\"{0}\", provider) called", format);
            return "arbitrary value";
        }
    }
    
    class MyFormatProvider : IFormatProvider
    {
        public object GetFormat(Type formatType)
        {
            Console.WriteLine("Asked for {0}", formatType);
            return CultureInfo.CurrentCulture.GetFormat(formatType);
        }
    }
    
    class App
    {
        static void Main()
        {
            Console.WriteLine(
                string.Format(new MyFormatProvider(), "{0:foobar}", 
                    new MyCustomObject()));
        }
    }
    



    它打印这样的:

    It prints this:

    Asked for System.ICustomFormatter
    ToString("foobar", provider) called
    arbitrary value
    

    如果该格式提供更改为返回自定义格式,它接管:

    If the format provider is changed to return a custom formatter, it takes over:

    class MyFormatProvider : IFormatProvider
    {
        public object GetFormat(Type formatType)
        {
            Console.WriteLine("Asked for {0}", formatType);
            if (formatType == typeof(ICustomFormatter))
                return new MyCustomFormatter();
            return CultureInfo.CurrentCulture.GetFormat(formatType);
        }
    }
    
    class MyCustomFormatter : ICustomFormatter
    {
        public string Format(string format, object arg, IFormatProvider provider)
        {
            return string.Format("(format was \"{0}\")", format);
        }
    }
    



    运行时:

    When run:

    Asked for System.ICustomFormatter
    (format was "foobar")
    

    这篇关于C#:IFormattable,和的IFormatProvider ICustomFormatter,当之间的连接使用什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆