閱讀英文

共用方式為


IFormatProvider.GetFormat(Type) 方法

定義

傳回物件,這個物件為所指定型別提供格式化服務。

C#
public object GetFormat(Type formatType);
C#
public object? GetFormat(Type? formatType);

參數

formatType
Type

物件,指定要傳回的格式化物件的型別。

傳回

如果 IFormatProvider 實作可以提供該型別的物件則為 formatType 所指定的物件執行個體,否則為 null

範例

下列範例說明如何使用實作 介面和 方法的 GetFormat 類別 IFormatProvider 。 類別 AcctNumberFormat 會將 Int64 代表帳戶號碼的值轉換為格式化的 12 位數帳戶號碼。 如果 formatType 參數參考實作 的類別 ICustomFormatter ,則其 GetFormat 方法會傳回本身的參考,否則會 GetFormatnull 回 。

C#
public class AcctNumberFormat : IFormatProvider, ICustomFormatter
{
   private const int ACCT_LENGTH = 12;

   public object GetFormat(Type formatType)
   {
      if (formatType == typeof(ICustomFormatter))
         return this;
      else
         return null;
   }

   public string Format(string fmt, object arg, IFormatProvider formatProvider)
   {
      // Provide default formatting if arg is not an Int64.
      if (arg.GetType() != typeof(Int64))
         try {
            return HandleOtherFormats(fmt, arg);
         }
         catch (FormatException e) {
            throw new FormatException(String.Format("The format of '{0}' is invalid.", fmt), e);
         }

      // Provide default formatting for unsupported format strings.
      string ufmt = fmt.ToUpper(CultureInfo.InvariantCulture);
      if (!(ufmt == "H" || ufmt == "I"))
         try {
            return HandleOtherFormats(fmt, arg);
         }
         catch (FormatException e) {
            throw new FormatException(String.Format("The format of '{0}' is invalid.", fmt), e);
         }

      // Convert argument to a string.
      string result = arg.ToString();

      // If account number is less than 12 characters, pad with leading zeroes.
      if (result.Length < ACCT_LENGTH)
         result = result.PadLeft(ACCT_LENGTH, '0');
      // If account number is more than 12 characters, truncate to 12 characters.
      if (result.Length > ACCT_LENGTH)
         result = result.Substring(0, ACCT_LENGTH);

      if (ufmt == "I")                    // Integer-only format.
         return result;
      // Add hyphens for H format specifier.
      else                                         // Hyphenated format.
         return result.Substring(0, 5) + "-" + result.Substring(5, 3) + "-" + result.Substring(8);
   }

   private string HandleOtherFormats(string format, object arg)
   {
      if (arg is IFormattable)
         return ((IFormattable)arg).ToString(format, CultureInfo.CurrentCulture);
      else if (arg != null)
         return arg.ToString();
      else
         return String.Empty;
   }
}

然後,類別的 AcctNumberFormat 實例可以當做引數傳遞至提供格式化或剖析服務的方法。 例如,下列程式碼會將 AcctNumberFormat 類別傳遞至 String.Format(IFormatProvider, String, Object[]) 方法,以產生格式化的 12 位數帳戶號碼。

C#
using System;
using System.Globalization;

public enum DaysOfWeek { Monday=1, Tuesday=2 };

public class TestFormatting
{
   public static void Main()
   {
      long acctNumber;
      double balance;
      DaysOfWeek wday;
      string output;

      acctNumber = 104254567890;
      balance = 16.34;
      wday = DaysOfWeek.Monday;

      output = String.Format(new AcctNumberFormat(),
                             "On {2}, the balance of account {0:H} was {1:C2}.",
                             acctNumber, balance, wday);
      Console.WriteLine(output);

      wday = DaysOfWeek.Tuesday;
      output = String.Format(new AcctNumberFormat(),
                             "On {2}, the balance of account {0:I} was {1:C2}.",
                             acctNumber, balance, wday);
      Console.WriteLine(output);
   }
}
// The example displays the following output:
//       On Monday, the balance of account 10425-456-7890 was $16.34.
//       On Tuesday, the balance of account 104254567890 was $16.34.

備註

GetFormat 是一種回呼方法,可格式化和剖析方法,以擷取剖析作業中輸入字串格式的相關資訊,或在格式化作業中擷取輸出字串的格式。 formatType在 參數中,格式化或剖析方法會傳遞執行其作業所需的物件類型。 如果實作 IFormatProvider 可以提供這個格式設定或剖析物件,則會傳回該物件。 如果沒有,則會傳 null 回 。

例如,在對 方法的呼叫 Int32.ToString(IFormatProvider) 中,method 引數是一個 IFormatProvider 物件,提供目前整數實例字串表示格式的相關資訊。 當執行時間執行 方法時,它會呼叫 IFormatProvider 物件的 GetFormat 方法,並傳遞 Type 代表型別的 NumberFormatInfo 物件。 IFormatProvider如果物件可以提供 NumberFormatInfo 物件,則會傳回該物件。 如果它無法提供該型別的物件,則會傳 null 回 。

您可以在 IFormatProvider 提供自訂格式或剖析服務的類別中實作 介面和 GetFormat 方法。 接著,實 IFormatProvider 作會當做引數傳遞至任何剖析或格式化方法的多載,其類型為 IFormatProvider ,例如 String.Format(IFormatProvider, String, Object[])Int32.ToString(String, IFormatProvider)DateTime.Parse(String, IFormatProvider)

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0