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);
[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" 表示不带前导 "0x" 的十六进制格式 valueRepresents 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.

适用于

另请参阅