ICustomFormatter 인터페이스

정의

개체 값의 사용자 지정 서식 지정을 지원하는 메서드를 정의합니다.

public interface class ICustomFormatter
public interface ICustomFormatter
[System.Runtime.InteropServices.ComVisible(true)]
public interface ICustomFormatter
type ICustomFormatter = interface
[<System.Runtime.InteropServices.ComVisible(true)>]
type ICustomFormatter = interface
Public Interface ICustomFormatter
특성

예제

다음 예제에서는 정수 값의 ICustomFormatter 이진, 8진수 및 16진수 서식을 허용하도록 를 구현합니다. 이 예제에서 단일 클래스인 는 MyFormatterIFormatProvider를 모두 ICustomFormatter 구현합니다. 해당 IFormatProvider.GetFormat 메서드는 매개 변수가 형식을 formatType 나타내는지 여부를 결정합니다 ICustomFormatter . 이 MyFormatter 경우 는 자체의 instance 반환하고, 그렇지 않으면 를 반환합니다null. 해당 구현은 ICustomFormatter.Format format 매개 변수가 지원되는 세 가지 형식 문자열(이진의 경우 "B", 8진수의 경우 "O", 16진수의 경우 "H") 중 하나인지 여부를 결정하고 매개 변수의 arg 형식을 적절하게 지정합니다. 그렇지 않으면 가 이 아닌 경우 arg 매개 변수의 IFormattable.ToString 구현(있는 경우) 또는 매개 변수가 없는 ToString 메서드(없는 경우)를 호출 argnull합니다. argnull이면 메서드에서 String.Empty을 반환합니다.

using System;
using System.Globalization;
using System.Numerics;

public class MyFormatter : IFormatProvider, ICustomFormatter
{
    // IFormatProvider.GetFormat implementation.
    public object GetFormat(Type formatType)
    {
        // Determine whether custom formatting object is requested.
        if (formatType == typeof(ICustomFormatter))
            return this;
        else
            return null;
    }

    // Format number in binary (B), octal (O), or hexadecimal (H).
    public string Format(string format, object arg, IFormatProvider formatProvider)
    {
        // Handle format string.
        int baseNumber;
        // Handle null or empty format string, string with precision specifier.
        string thisFmt = String.Empty;
        // Extract first character of format string (precision specifiers
        // are not supported).
        if (!String.IsNullOrEmpty(format))
            thisFmt = format.Length > 1 ? format.Substring(0, 1) : format;

        // Get a byte array representing the numeric value.
        byte[] bytes;
        if (arg is sbyte)
        {
            string byteString = ((sbyte)arg).ToString("X2");
            bytes = new byte[1] { Byte.Parse(byteString, System.Globalization.NumberStyles.HexNumber) };
        }
        else if (arg is byte)
        {
            bytes = new byte[1] { (byte)arg };
        }
        else if (arg is short)
        {
            bytes = BitConverter.GetBytes((short)arg);
        }
        else if (arg is int)
        {
            bytes = BitConverter.GetBytes((int)arg);
        }
        else if (arg is long)
        {
            bytes = BitConverter.GetBytes((long)arg);
        }
        else if (arg is ushort)
        {
            bytes = BitConverter.GetBytes((ushort)arg);
        }
        else if (arg is uint)
        {
            bytes = BitConverter.GetBytes((uint)arg);
        }
        else if (arg is ulong)
        {
            bytes = BitConverter.GetBytes((ulong)arg);
        }
        else if (arg is BigInteger)
        {
            bytes = ((BigInteger)arg).ToByteArray();
        }
        else
        {
            try
            {
                return HandleOtherFormats(format, arg);
            }
            catch (FormatException e)
            {
                throw new FormatException(String.Format("The format of '{0}' is invalid.", format), e);
            }
        }

        switch (thisFmt.ToUpper())
        {
            // Binary formatting.
            case "B":
                baseNumber = 2;
                break;
            case "O":
                baseNumber = 8;
                break;
            case "H":
                baseNumber = 16;
                break;
            // Handle unsupported format strings.
            default:
                try
                {
                    return HandleOtherFormats(format, arg);
                }
                catch (FormatException e)
                {
                    throw new FormatException(String.Format("The format of '{0}' is invalid.", format), e);
                }
        }

        // Return a formatted string.
        string numericString = String.Empty;
        for (int ctr = bytes.GetUpperBound(0); ctr >= bytes.GetLowerBound(0); ctr--)
        {
            string byteString = Convert.ToString(bytes[ctr], baseNumber);
            if (baseNumber == 2)
                byteString = new String('0', 8 - byteString.Length) + byteString;
            else if (baseNumber == 8)
                byteString = new String('0', 4 - byteString.Length) + byteString;
            // Base is 16.
            else
                byteString = new String('0', 2 - byteString.Length) + byteString;

            numericString += byteString + " ";
        }
        return numericString.Trim();
    }

    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;
    }
}
open System
open System.Globalization
open System.Numerics

type MyFormatter() =
    interface IFormatProvider with
        // IFormatProvider.GetFormat implementation.
        member this.GetFormat(formatType: Type) =
            // Determine whether custom formatting object is requested.
            if formatType = typeof<ICustomFormatter> then
                this
            else
                null

    interface ICustomFormatter with
        // Format number in binary (B), octal (O), or hexadecimal (H).
        member this.Format(format, arg: obj, formatProvider: IFormatProvider) =
            // Handle null or empty format string, string with precision specifier.
            let thisFmt =
                // Extract first character of format string (precision specifiers
                // are not supported).
                if String.IsNullOrEmpty format |> not then
                    if format.Length > 1 then 
                        format.Substring(0, 1) 
                    else 
                        format
                else
                    String.Empty

            // Get a byte array representing the numeric value.
            let bytes =
                match arg with
                | :? sbyte as arg ->
                    let byteString = arg.ToString "X2"
                    Some [| Byte.Parse(byteString, NumberStyles.HexNumber) |]
                | :? byte as arg ->
                    Some [| arg |]
                | :? int16 as arg ->
                    BitConverter.GetBytes arg
                    |> Some
                | :? int as arg ->
                    BitConverter.GetBytes arg
                    |> Some
                | :? int64 as arg ->
                    BitConverter.GetBytes arg
                    |> Some
                | :? uint16 as arg ->
                    BitConverter.GetBytes arg
                    |> Some
                | :? uint as arg ->
                    BitConverter.GetBytes arg
                    |> Some
                | :? uint64 as arg ->
                    BitConverter.GetBytes arg
                    |> Some
                | :? bigint as arg ->
                    arg.ToByteArray()
                    |> Some
                | _ ->
                    None
            let baseNumber =
                match thisFmt.ToUpper() with
                    // Binary formatting.
                    | "B" -> Some 2
                    | "O" -> Some 8
                    | "H" -> Some 16
                    // Handle unsupported format strings.
                    | _ -> None

            match bytes, baseNumber with
            | Some bytes, Some baseNumber ->
                // Return a formatted string.
                let mutable numericString = String.Empty
                for i = bytes.GetUpperBound 0 to bytes.GetLowerBound 0 do
                    let byteString = Convert.ToString(bytes[i], baseNumber)
                    let byteString =
                        match baseNumber with
                        | 2 ->
                            String('0', 8 - byteString.Length) + byteString
                        | 8 ->
                            String('0', 4 - byteString.Length) + byteString
                        // Base is 16.
                        | _ ->
                            String('0', 2 - byteString.Length) + byteString
                    numericString <- numericString + byteString + " "
                numericString.Trim()
            | _ ->
                try
                    this.HandleOtherFormats(format, arg)
                with :? FormatException as e ->
                    raise (FormatException($"The format of '{format}' is invalid.", e))
                    
    member private this.HandleOtherFormats(format, arg: obj) =
        match arg with
        | :? IFormattable as arg ->
            arg.ToString(format, CultureInfo.CurrentCulture)
        | null ->
            String.Empty
        | _ ->
            string arg
Imports System.Globalization
Imports System.Numerics

Public Class MyFormatter : Implements IFormatProvider, ICustomFormatter
   ' IFormatProvider.GetFormat implementation.
   Public Function GetFormat(formatType As Type) As Object _
                   Implements IFormatProvider.GetFormat
      ' Determine whether custom formatting object is requested.
      If formatType Is GetType(ICustomFormatter) Then
         Return Me
      Else
         Return Nothing
      End If
   End Function   

   ' Format number in binary (B), octal (O), or hexadecimal (H).
   Public Function Format(fmt As String, arg As Object, _
                          formatProvider As IFormatProvider) As String _
                   Implements ICustomFormatter.Format

     ' Handle format string.
      Dim base As Integer
      ' Handle null or empty format string, string with precision specifier.
      Dim thisFmt As String = String.Empty
      ' Extract first character of format string (precision specifiers
      ' are not supported by MyFormatter).
      If Not String.IsNullOrEmpty(fmt) Then
         thisFmt = CStr(IIf(fmt.Length > 1, fmt.Substring(0, 1), fmt))
      End If
         


      ' Get a byte array representing the numeric value.
      Dim bytes() As Byte
      If TypeOf(arg) Is SByte Then
         Dim byteString As String = CType(arg, SByte).ToString("X2")
         bytes = New Byte(0) { Byte.Parse(byteString, System.Globalization.NumberStyles.HexNumber ) }
      ElseIf TypeOf(arg) Is Byte Then
         bytes = New Byte(0) { CType(arg, Byte) }
      ElseIf TypeOf(arg) Is Int16 Then
         bytes = BitConverter.GetBytes(CType(arg, Int16))
      ElseIf TypeOf(arg) Is Int32 Then
         bytes = BitConverter.GetBytes(CType(arg, Int32))
      ElseIf TypeOf(arg) Is Int64 Then
         bytes = BitConverter.GetBytes(CType(arg, Int64))
      ElseIf TypeOf(arg) Is UInt16 Then
         bytes = BitConverter.GetBytes(CType(arg, UInt16))
      ElseIf TypeOf(arg) Is UInt32 Then
         bytes = BitConverter.GetBytes(CType(arg, UInt64))
      ElseIf TypeOf(arg) Is UInt64 Then
         bytes = BitConverter.GetBytes(CType(arg, UInt64))                  
      ElseIf TypeOf(arg) Is BigInteger Then
         bytes = CType(arg, BigInteger).ToByteArray()
      Else
         Try 
            Return HandleOtherFormats(fmt, arg) 
         Catch e As FormatException 
            Throw New FormatException(String.Format("The format of '{0}' is invalid.", fmt), e)
         End Try
      End If

      Select Case thisFmt.ToUpper()
         ' Binary formatting.
         Case "B"
            base = 2        
         Case "O"
            base = 8
         Case "H"
            base = 16
         ' Handle unsupported format strings.
         Case Else
            Try 
               Return HandleOtherFormats(fmt, arg) 
            Catch e As FormatException 
               Throw New FormatException(String.Format("The format of '{0}' is invalid.", fmt), e)
            End Try
      End Select
      
      ' Return a formatted string.
      Dim numericString As String = String.Empty
      For ctr As Integer = bytes.GetUpperBound(0) To bytes.GetLowerBound(0) Step -1
         Dim byteString As String = Convert.ToString(bytes(ctr), base)
         If base = 2 Then
            byteString = New String("0"c, 8 - byteString.Length) + byteString
         ElseIf base = 8 Then
            byteString = New String("0"c, 4 - byteString.Length) + byteString
         ' Base is 16.
         Else     
            byteString = New String("0"c, 2 - byteString.Length) + byteString
         End If
         numericString +=  byteString + " "
      Next
      Return numericString.Trim()
   End Function
   
   Private Function HandleOtherFormats(fmt As String, arg As Object) As String
      If TypeOf arg Is IFormattable Then
         Return DirectCast(arg, IFormattable).ToString(fmt, CultureInfo.CurrentCulture)
      ElseIf arg IsNot Nothing Then
         Return arg.ToString()
      Else
         Return String.Empty
      End If
   End Function
End Class

MyFormatter는 다음 예제와 같이 개체를 메서드의 Format 매개 변수로 provider 전달 MyFormatter 하여 사용자 지정 서식을 제공하는 데 사용할 수 있습니다.

public class Example
{
    public static void Main()
    {
        Console.WindowWidth = 100;

        byte byteValue = 124;
        Console.WriteLine(String.Format(new MyFormatter(),
                                        "{0} (binary: {0:B}) (hex: {0:H})", byteValue));

        int intValue = 23045;
        Console.WriteLine(String.Format(new MyFormatter(),
                                        "{0} (binary: {0:B}) (hex: {0:H})", intValue));

        ulong ulngValue = 31906574882;
        Console.WriteLine(String.Format(new MyFormatter(),
                                        "{0}\n   (binary: {0:B})\n   (hex: {0:H})",
                                        ulngValue));

        BigInteger bigIntValue = BigInteger.Multiply(Int64.MaxValue, 2);
        Console.WriteLine(String.Format(new MyFormatter(),
                                        "{0}\n   (binary: {0:B})\n   (hex: {0:H})",
                                        bigIntValue));
    }
}
// The example displays the following output:
//    124 (binary: 01111100) (hex: 7c)
//    23045 (binary: 00000000 00000000 01011010 00000101) (hex: 00 00 5a 05)
//    31906574882
//       (binary: 00000000 00000000 00000000 00000111 01101101 11000111 10110010 00100010)
//       (hex: 00 00 00 07 6d c7 b2 22)
//    18446744073709551614
//       (binary: 00000000 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111110)
//       (hex: 00 ff ff ff ff ff ff ff fe)
Console.WindowWidth <- 100

let byteValue = 124uy
String.Format(MyFormatter(), "{0} (binary: {0:B}) (hex: {0:H})", byteValue)
|> printfn "%s"

let intValue = 23045
String.Format(MyFormatter(), "{0} (binary: {0:B}) (hex: {0:H})", intValue)
|> printfn "%s"

let ulngValue = 31906574882uL
String.Format(MyFormatter(), "{0}\n   (binary: {0:B})\n   (hex: {0:H})", ulngValue)
|> printfn "%s"

let bigIntValue = BigInteger.Multiply(Int64.MaxValue, 2)
String.Format(MyFormatter(), "{0}\n   (binary: {0:B})\n   (hex: {0:H})", bigIntValue)
|> printfn "%s"

// The example displays the following output:
//    124 (binary: 01111100) (hex: 7c)
//    23045 (binary: 00000000 00000000 01011010 00000101) (hex: 00 00 5a 05)
//    31906574882
//       (binary: 00000000 00000000 00000000 00000111 01101101 11000111 10110010 00100010)
//       (hex: 00 00 00 07 6d c7 b2 22)
//    18446744073709551614
//       (binary: 00000000 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111110)
//       (hex: 00 ff ff ff ff ff ff ff fe)
Public Module Example
   Public Sub Main
      Console.WindowWidth = 100
      
      Dim byteValue As Byte = 124
      Console.WriteLine(String.Format(New MyFormatter(), _
                                      "{0} (binary: {0:B}) (hex: {0:H})", byteValue))
      
      Dim intValue As Integer = 23045
      Console.WriteLine(String.Format(New MyFormatter(), _
                                      "{0} (binary: {0:B}) (hex: {0:H})", intValue))
      
      Dim ulngValue As ULong = 31906574882
      Console.WriteLine(String.Format(New MyFormatter(), _
                                      "{0} {1}   (binary: {0:B}) {1}   (hex: {0:H})", _
                                      ulngValue, vbCrLf))

      Dim bigIntValue As BigInteger = BigInteger.Multiply(Int64.MaxValue, 2)
      Console.WriteLine(String.Format(New MyFormatter(), _
                                      "{0} {1}   (binary: {0:B}) {1}   (hex: {0:H})", _
                                      bigIntValue, vbCrLf))
   End Sub
End Module
' The example displays the following output:
'    124 (binary: 01111100) (hex: 7c)
'    23045 (binary: 00000000 00000000 01011010 00000101) (hex: 00 00 5a 05)
'    31906574882
'       (binary: 00000000 00000000 00000000 00000111 01101101 11000111 10110010 00100010)
'       (hex: 00 00 00 07 6d c7 b2 22)
'    18446744073709551614
'       (binary: 00000000 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111110)
'       (hex: 00 ff ff ff ff ff ff ff fe)

설명

합니다 ICustomFormatter 단일 메서드를 포함 하는 인터페이스 ICustomFormatter.Format합니다. 이 인터페이스가 참조 또는 값 형식으로 구현되면 메서드는 Format 개체 값의 사용자 지정 형식 문자열 표현을 반환합니다.

일반적으로 ICustomFormatter 인터페이스는 매개 변수를 포함하는 두 .NET Framework 복합 문자열 서식 지정 메서드의 동작을 사용자 지정하기 위해 인터페이스와 IFormatProvider 함께 IFormatProvider 구현됩니다. 특히 인터페이스는 ICustomFormatterStringBuilder.AppendFormat(IFormatProvider, String, Object[]) 메서드에 전달된 개체 값의 사용자 지정 서식을 String.Format(IFormatProvider, String, Object[]) 제공할 수 있습니다.

개체 값의 사용자 지정 표현을 제공하려면 다음을 수행해야 합니다.

  1. 인터페이스와 해당 단일 멤버인 메서드를 구현 ICustomFormatter 하는 클래스를 정의합니다 Format .

  2. 인터페이스와 해당 단일 멤버인 메서드를 구현 IFormatProvider 하는 클래스를 정의합니다 GetFormat . 메서드는 GetFormat 구현의 instance 반환합니다ICustomFormatter. 종종 단일 클래스는 및 IFormatProvider를 모두 ICustomFormatter 구현합니다. 이 경우 클래스의 GetFormat 구현은 자체의 instance 반환합니다.

  3. 구현을 IFormatProvider 메서드 또는 provider 비교 가능한 메서드의 String.Format(IFormatProvider, String, Object[]) 인수로 전달합니다.

그런 다음 .NET 라이브러리 메서드는 자체 서식 대신 사용자 지정 서식을 사용합니다.

구현자 참고

공용 언어 런타임은 복합 형식 문자열의 모든 형식 항목에 구현을 사용 ICustomFormatter 하려고 시도합니다. 따라서 구현이 ICustomFormatter 호출되어 처리하도록 설계되지 않은 개체 또는 값에 서식 지정 서비스를 제공할 것으로 예상해야 합니다. 이러한 경우 메서드는 Format(String, Object, IFormatProvider) 해당 개체 또는 값에 적절한 서식 지정 메서드를 호출해야 합니다.

구현에는 내장 및 확장의 ICustomFormatter 두 가지 종류가 있습니다.

내장 함수 구현에는 애플리케이션 정의 개체에 대 한 사용자 지정 서식 지정을 제공 하는 구현입니다. 이 경우 구현에는 다음이 포함되어야 합니다.

  • 개체의 서식을 정의하는 형식 문자열의 정의입니다. 형식 문자열은 선택 사항입니다. 일반적으로 "G" 또는 "g" 형식 문자열은 일반(또는 가장 일반적으로 사용되는) 형식을 정의합니다. 그러나 선택한 형식 문자열을 자유롭게 정의할 수 있습니다. 또한 대/소문자를 구분하는지 또는 대/소문자를 구분하지 않는지 자유롭게 결정할 수 있습니다.

  • 메서드에 전달된 Format(String, Object, IFormatProvider) 개체의 형식이 애플리케이션 정의 형식인지 확인하는 테스트입니다. 그렇지 않은 경우 개체의 IFormattable 구현(있는 경우) 또는 메서드(없는 경우)를 ToString() 호출해야 합니다. 이러한 메서드 호출이 throw할 수 있는 예외를 처리할 준비가 되어 있어야 합니다.

  • 구현에서 형식 문자열을 지원하는 경우 null 형식 문자열을 처리하는 코드입니다. 가장 일반적인 방법은 null 형식 문자열을 일반 형식 지정자로 바꾸는 것입니다.

  • 구현에서 지원하는 모든 형식 문자열을 처리하는 코드입니다.

  • 지원하지 않는 형식 문자열을 처리하는 코드입니다. 가장 일반적인 방법은 기본 서식을 제공할 수 있지만 을 FormatExceptionthrow하는 것입니다.

확장 구현은 이미 서식 지원이 있는 형식에 대한 사용자 지정 서식을 제공하는 구현입니다. 예를 들어 특정 숫자 사이에 하이픈을 사용하여 정수 형식의 서식을 지정하는 을 정의 CustomerNumberFormatter 할 수 있습니다. 이 경우 구현에는 다음이 포함되어야 합니다.

  • 개체의 서식을 확장하는 형식 문자열의 정의입니다. 이러한 형식 문자열은 필수이지만 형식의 기존 형식 문자열과 충돌해서는 안 됩니다. 예를 들어 형식에 대한 Int32 서식을 확장하는 경우 "C", "D", "E", "F" 및 "G" 형식 지정자를 구현해서는 안 됩니다.

  • 메서드에 전달된 Format(String, Object, IFormatProvider) 개체의 형식이 확장에서 지원하는 형식이라는 테스트입니다. 그렇지 않은 경우 개체의 IFormattable 구현(있는 경우) 또는 개체의 매개 변수 없는 ToString() 메서드(없는 경우)를 호출합니다. 이러한 메서드 호출이 throw할 수 있는 예외를 처리할 준비가 되어 있어야 합니다.

  • 확장에서 지원하는 모든 형식 문자열을 처리하는 코드입니다.

  • 확장에서 지원하지 않는 형식 문자열을 처리하는 코드입니다. 이러한 형식은 형식의 IFormattable 구현에 전달되어야 합니다. 이러한 메서드 호출이 throw할 수 있는 예외를 처리할 준비가 되어 있어야 합니다.

메서드

Format(String, Object, IFormatProvider)

지정된 형식 및 문화권별 형식 지정 정보를 사용하여 지정된 개체의 값을 해당 문자열 표현으로 변환합니다.

적용 대상

추가 정보