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 の文字列形式。

属性

例外

enumTypevalue、または format パラメーターが nullです。

enumType パラメーターが Enum 型ではありません。

- または -

value は、型が enumType とは異なる列挙体からのものです。

- または -

value の型は、enumType の基となる型ではありません。

format パラメーターに正しくない値が含まれています。

format は "X" と等しいですが、列挙型が不明です。

または

.NET 8 以降のバージョン: enumType は、ブール型に基づく列挙型です。

次の例は、EnumコンテキストでFormatを使用する方法を示しています。

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 示します。

Format 説明
"G" または "g" valueが名前付き列挙定数と等しい場合は、その定数の名前が返されます。それ以外の場合は、valueに相当する 10 進数が返されます。

たとえば、唯一の列挙型定数の名前が Red で、その値が 1 であるとします。 valueが 1 と指定されている場合、この形式は "Red" を返します。 ただし、valueが 2 と指定されている場合、この形式は "2" を返します。

- または -

FlagsAttributeカスタム属性が列挙に適用されている場合、valueは 1 つ以上のビットで構成される 1 つ以上のフラグを含むビットフィールドとして扱われます。

valueが名前付き列挙定数の組み合わせと等しい場合は、その定数の名前の区切り記号で区切られたリストが返されます。 value はフラグを検索し、最大値を持つフラグから最小値に移動します。 valueのビットフィールドに対応するフラグごとに、定数の名前が区切り記号で区切られたリストに連結されます。 そのフラグの値は、その後の検索から除外され、次のフラグに対して検索が続行されます。

valueが名前付き列挙定数の組み合わせと等しくない場合は、valueに相当する 10 進数が返されます。
"X" または "x" valueを先頭に "0x" がない 16 進数形式で表します。
"D" または "d" valueを 10 進数形式で表します。
"F" または "f" "G" または "g" と同じように動作しますが、Enum宣言にFlagsAttributeが存在する必要がない点が異なります。

適用対象

こちらもご覧ください