Enum.Format(Type, Object, String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
根據指定的格式,將指定列舉型別的指定值轉換為其相等字串表示。
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, , , 為 valueformatnull。
該 format 參數包含無效值。
範例
以下範例說明在 的Format情境下 的Enum使用。
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 | Description |
|---|---|
| "G" 或 "g" | 若 value 等於一個已列舉的命名常數,則返回該常數的名稱;否則,返回的 value 十進位等價值。例如,假設唯一被列舉的常數名為 Red,其值為 1。 若 value 指定為 1,則此格式回傳「紅色」。 然而,若 value 被指定為 2,則此格式會回傳「2」。-或- 若 FlagsAttribute 套用自訂屬性於枚舉, value 則視為包含一個或多個由一個或多個位元組成的旗標的位元欄位。若 value 等於一組命名的列舉常數,則回傳一個分隔符的這些常數名稱列表。
value 搜尋旗標,從最大值到最小。 對於對應於 中 value某個位元欄位的旗標,常數的名稱會串接到分隔符的列表中。 該旗幟的價值因此被排除在後續考慮之外,搜尋下一旗幟的過程繼續進行。若 value 不等於一組命名的列舉常數組合,則返回 的 value 十進位等價值。 |
| "X" 或 "x" | 以無前置「0x」的十六進位格式表示 value 。 |
| "D" 或 "d" | 以十進位形式表示 value 。 |
| "F" 或 "f" | 行為與「G」或「g」相同,但 FlagsAttribute 聲明中不必出現 Enum 。 |