Enum.Format(Type, Object, String) 方法

定義

根據指定的格式,將指定列舉類型的指定值轉換為相等的字串表示。Converts the specified value of a specified enumerated type to its equivalent string representation according to the specified format.

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
Public Shared Function Format (enumType As Type, value As Object, format As String) As String

參數

enumType
Type

要轉換的數值列舉類型。The enumeration type of the value to convert.

value
Object

要進行轉換的值。The value to convert.

format
String

要使用的輸出格式。The output format to use.

傳回

value 的字串表示。A string representation of value.

屬性

例外狀況

enumTypevalueformat 參數是 nullThe enumType, value, or format parameter is null.

enumType 參數不是 Enum 類型。The enumType parameter is not an Enum type.

-或--or- value 來自與 enumType 類型不同的列舉。The value is from an enumeration that differs in type from enumType.

-或--or- 類型 value 不是 enumType 的基礎類型。The type of value is not an underlying type of enumType.

format 參數包含無效的值。The format parameter contains an invalid value.

format 等於 "X",但列舉類型未知。format equals "X", but the enumeration type is unknown.

範例

下列範例說明如何在 Enum的內容中使用 FormatThe following example illustrates the use of Format in the context of 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.
 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 參數的有效值。The following table shows the valid values for the format parameter.

格式Format 描述Description
"G" 或 "g""G" or "g" 如果 value 等於已命名的列舉常數,則會傳回該常數的名稱;否則,會傳回 value 的十進位對等。If value is equal to a named enumerated constant, the name of that constant is returned; otherwise, the decimal equivalent of value is returned.

例如,假設唯一列舉的常數名為 Red,而其值為1。For example, suppose the only enumerated constant is named Red, and its value is 1. 如果 value 指定為1,則此格式會傳回 "Red"。If value is specified as 1, this format returns "Red". 不過,如果 value 指定為2,則此格式會傳回 "2"。However, if value is specified as 2, this format returns "2".

-或--or-

如果 FlagsAttribute 自訂屬性套用至列舉,value 會被視為位欄位,其中包含一或多個由一或多個位組成的旗標。If the FlagsAttribute custom attribute is applied to the enumeration, value is treated as a bit field that contains one or more flags that consist of one or more bits.

如果 value 等於已命名列舉常數的組合,則會傳回這些常數名稱的分隔符號分隔清單。If value is equal to a combination of named enumerated constants, a delimiter-separated list of the names of those constants is returned. value 會搜尋旗標,從具有最大值的旗標到最小值。value is searched for flags, going from the flag with the largest value to the smallest value. 針對每個對應至 value中位欄位的旗標,常數的名稱會串連成以分隔符號分隔的清單。For each flag that corresponds to a bit field in value, the name of the constant is concatenated to the delimiter-separated list. 該旗標的值接著會從進一步考慮中排除,而搜尋會繼續進行下一個旗標。The value of that flag is then excluded from further consideration, and the search continues for the next flag.

如果 value 不等於已命名列舉常數的組合,則會傳回 value 的十進位對等。If value is not equal to a combination of named enumerated constants, the decimal equivalent of value is returned.
"X" 或 "x""X" or "x" 代表十六進位格式的 value,但不含前置的 "0x"。Represents value in hexadecimal format without a leading "0x".
"D" 或 "d""D" or "d" 代表十進位格式的 valueRepresents value in decimal form.
"F" 或 "f""F" or "f" 的行為與 "G" 或 "g" 相同,不同之處在于 FlagsAttribute 不一定要出現在 Enum 宣告上。Behaves identically to "G" or "g", except that the FlagsAttribute is not required to be present on the Enum declaration.

適用於

另請參閱