Enum.Format(Type, Object, String) 메서드

정의

열거 형식이 지정된 특정 값을 특정 서식에 따라 해당 문자열 표현으로 변환합니다.

public:
 static System::String ^ Format(Type ^ enumType, System::Object ^ value, System::String ^ format);
public static string Format (Type enumType, object value, string format);
[System.Runtime.InteropServices.ComVisible(true)]
public static string Format (Type enumType, object value, string format);
static member Format : Type * obj * string -> string
[<System.Runtime.InteropServices.ComVisible(true)>]
static member Format : Type * obj * string -> string
Public Shared Function Format (enumType As Type, value As Object, format As String) As String

매개 변수

enumType
Type

변환할 값의 열거형 형식입니다.

value
Object

변환할 값입니다.

format
String

사용할 출력 서식입니다.

반환

value의 문자열 표현입니다.

특성

예외

enumType, value 또는 format 매개 변수는 null입니다.

enumType 매개 변수가 Enum 형식이 아닙니다.

또는

valueenumType의 형식과 다른 열거형에서 얻은 것입니다.

또는

value의 형식은 enumType의 내부 형식이 아닙니다.

format 매개 변수에 잘못된 값이 포함되어 있습니다.

format이 "X"와 같지만 열거형 형식을 알 수 없습니다.

또는

.NET 8 이상 버전: enumType 부울 지원 열거형 형식입니다.

예제

다음 예제에서는 의 컨텍스트에서 를 Format 사용하는 것을 보여 줍니다 Enum.

using namespace System;
public enum class Colors
{
   Red, Green, Blue, Yellow
};

int main()
{
   Colors myColor = Colors::Blue;
   Console::WriteLine(  "My favorite color is {0}.", myColor );
   Console::WriteLine(  "The value of my favorite color is {0}.", Enum::Format( Colors::typeid, myColor,  "d" ) );
   Console::WriteLine(  "The hex value of my favorite color is {0}.", Enum::Format( Colors::typeid, myColor,  "x" ) );
}
// The example displays the folowing output:
//    My favorite color is Blue.
//    The value of my favorite color is 2.
//    The hex value of my favorite color is 00000002.
using System;

enum Colors { Red, Green, Blue, Yellow };

public class FormatTest {
    public static void Main() {
        Colors myColor = Colors.Blue;

        Console.WriteLine("My favorite color is {0}.", myColor);
        Console.WriteLine("The value of my favorite color is {0}.", Enum.Format(typeof(Colors), myColor, "d"));
        Console.WriteLine("The hex value of my favorite color is {0}.", Enum.Format(typeof(Colors), myColor, "x"));
    }
}
// The example displays the following output:
//    My favorite color is Blue.
//    The value of my favorite color is 2.
//    The hex value of my favorite color is 00000002.
open System

type Colors =
    | Red = 0
    | Green = 1
    | Blue = 2
    | Yellow = 3

let myColor = Colors.Blue

printfn $"My favorite color is {myColor}."
printfn $"""The value of my favorite color is {Enum.Format(typeof<Colors>, myColor, "d")}."""
printfn $"""The hex value of my favorite color is {Enum.Format(typeof<Colors>, myColor, "x")}."""
// The example displays the following output:
//    My favorite color is Blue.
//    The value of my favorite color is 2.
//    The hex value of my favorite color is 00000002.
 Enum Colors
     Red
     Green
     Blue
     Yellow    
 End Enum
    
Public Class FormatTest
    Public Shared Sub Main()
        Dim myColor As Colors = Colors.Blue
        
        Console.WriteLine("My favorite color is {0}.", myColor)
        Console.WriteLine("The value of my favorite color is {0}.", [Enum].Format(GetType(Colors), myColor, "d"))
        Console.WriteLine("The hex value of my favorite color is {0}.", [Enum].Format(GetType(Colors), myColor, "x"))
    End Sub 
End Class 
' The example displays the following output:
'    My favorite color is Blue.
'    The value of my favorite color is 2.
'    The hex value of my favorite color is 00000002.

설명

다음 표에서는 매개 변수에 대한 유효한 값을 보여 있습니다 format .

서식 설명
"G" 또는 "g" 가 명명된 열거형 상수와 같으면 value 해당 상수의 이름이 반환되고, 그렇지 않으면 의 value 10진수가 반환됩니다.

예를 들어 열거형 상수만 Red이고 해당 값이 1이라고 가정합니다. 가 1로 지정되면 value 이 형식은 "Red"를 반환합니다. 그러나 가 2로 지정된 경우 value 이 형식은 "2"를 반환합니다.

또는

FlagsAttribute 사용자 지정 특성이 열거형 value 에 적용되는 경우 는 하나 이상의 비트로 구성된 하나 이상의 플래그를 포함하는 비트 필드로 처리됩니다.

가 명명된 열거형 상수의 조합과 같으면 value 해당 상수의 이름에 대한 구분 기호로 구분된 목록이 반환됩니다. value 는 가장 큰 값을 가진 플래그에서 가장 작은 값으로 가는 플래그를 검색합니다. 의 비트 필드에 value해당하는 각 플래그에 대해 상수의 이름이 구분 기호로 구분된 목록에 연결됩니다. 그런 다음 해당 플래그의 값은 추가 고려 사항에서 제외되고 다음 플래그에 대한 검색이 계속됩니다.

가 명명된 열거형 상수의 조합과 같지 않으면 value 의 10진수가 value 반환됩니다.
"X" 또는 "x" value 선행 "0x"가 없는 16진수 형식을 나타냅니다.
"D" 또는 "d" value 10진수 형식을 나타냅니다.
"F" 또는 "f" 가 선언에 있을 필요가 없다는 점을 제외하고 FlagsAttribute "G" 또는 "g"와 Enum 동일하게 동작합니다.

적용 대상

추가 정보