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 的字串表示。

屬性

例外狀況

enumTypevalueformat 參數是 null

enumType 參數不是 Enum 類型。

-或-

value 來自與 enumType 類型不同的列舉。

-或-

類型 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 十進位對等專案。

例如,假設唯一的列舉常數命名為 Red,而其值為 1。 如果 value 指定為 1,則此格式會傳回 「Red」。 不過,如果 value 指定為 2,則此格式會傳回 「2」。

-或-

FlagsAttribute如果自訂屬性套用至列舉, value 則會被視為包含一或多個由一或多個位組成的旗標的位欄位。

如果 value 等於具名列舉常數的組合,則會傳回這些常數名稱的分隔符號分隔清單。 value 會搜尋旗標,從具有最大值的旗標移至最小值。 對於對應至 中 value 位欄位的每個旗標,常數的名稱會串連至分隔符號分隔清單。 然後,該旗標的值會從進一步考慮中排除,而且搜尋會繼續下一個旗標。

如果 value 不等於具名列舉常數的組合,則會傳回 的 value 十進位對等專案。
"X" 或 "x" value 十六進位格式表示,不含前置 「0x」。
"D" 或 "d" value 十進位格式表示。
"F" 或 "f" 行為與 「G」 或 「g」 相同,不同之處在于 FlagsAttribute 宣告上 Enum 不需要存在 。

適用於

另請參閱