<bdo id='ffbn5'></bdo><ul id='ffbn5'></ul>
    1. <tfoot id='ffbn5'></tfoot>
      1. <small id='ffbn5'></small><noframes id='ffbn5'>

      2. <i id='ffbn5'><tr id='ffbn5'><dt id='ffbn5'><q id='ffbn5'><span id='ffbn5'><b id='ffbn5'><form id='ffbn5'><ins id='ffbn5'></ins><ul id='ffbn5'></ul><sub id='ffbn5'></sub></form><legend id='ffbn5'></legend><bdo id='ffbn5'><pre id='ffbn5'><center id='ffbn5'></center></pre></bdo></b><th id='ffbn5'></th></span></q></dt></tr></i><div id='ffbn5'><tfoot id='ffbn5'></tfoot><dl id='ffbn5'><fieldset id='ffbn5'></fieldset></dl></div>
        <legend id='ffbn5'><style id='ffbn5'><dir id='ffbn5'><q id='ffbn5'></q></dir></style></legend>

        C#:IFormattable、IFormatProvider 和 ICustomFormatter 之间的连接,以及何

        时间:2023-05-21
      3. <tfoot id='aKFQV'></tfoot>
            <bdo id='aKFQV'></bdo><ul id='aKFQV'></ul>
              <tbody id='aKFQV'></tbody>
              <i id='aKFQV'><tr id='aKFQV'><dt id='aKFQV'><q id='aKFQV'><span id='aKFQV'><b id='aKFQV'><form id='aKFQV'><ins id='aKFQV'></ins><ul id='aKFQV'></ul><sub id='aKFQV'></sub></form><legend id='aKFQV'></legend><bdo id='aKFQV'><pre id='aKFQV'><center id='aKFQV'></center></pre></bdo></b><th id='aKFQV'></th></span></q></dt></tr></i><div id='aKFQV'><tfoot id='aKFQV'></tfoot><dl id='aKFQV'><fieldset id='aKFQV'></fieldset></dl></div>

                • <legend id='aKFQV'><style id='aKFQV'><dir id='aKFQV'><q id='aKFQV'></q></dir></style></legend>

                  <small id='aKFQV'></small><noframes id='aKFQV'>

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

                  问题描述

                  限时送ChatGPT账号..

                  IFormattableIFormatProviderICustomFormatter有什么区别和联系,什么时候用?一个简单的实现示例也会非常好.

                  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 框架中使用它,而是什么时候我自己实现这些,在这种情况下,什么类通常会实现什么接口以及如何正确地实现它.

                  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格式的对象,即{0:xxx中的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 在委托给对象的 IFormattable.Format 之前检查提供程序的 ICustomFormatter,这反过来可能会询问 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.FormatIFormatProvider 请求什么,以及控制流程是如何进行的:

                      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()));
                          }
                      }
                      

                      它打印这个:

                      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);
                          }
                      }
                      

                      运行时:

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

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

                  <legend id='gcR3l'><style id='gcR3l'><dir id='gcR3l'><q id='gcR3l'></q></dir></style></legend>

                    <i id='gcR3l'><tr id='gcR3l'><dt id='gcR3l'><q id='gcR3l'><span id='gcR3l'><b id='gcR3l'><form id='gcR3l'><ins id='gcR3l'></ins><ul id='gcR3l'></ul><sub id='gcR3l'></sub></form><legend id='gcR3l'></legend><bdo id='gcR3l'><pre id='gcR3l'><center id='gcR3l'></center></pre></bdo></b><th id='gcR3l'></th></span></q></dt></tr></i><div id='gcR3l'><tfoot id='gcR3l'></tfoot><dl id='gcR3l'><fieldset id='gcR3l'></fieldset></dl></div>

                  • <tfoot id='gcR3l'></tfoot>
                        <tbody id='gcR3l'></tbody>

                      <small id='gcR3l'></small><noframes id='gcR3l'>

                        <bdo id='gcR3l'></bdo><ul id='gcR3l'></ul>