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 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,则此格式返回“Red”。 但是,如果 value 指定为 2,则此格式返回“2”。

-或-

FlagsAttribute如果自定义属性应用于枚举,value则被视为包含一个或多个由一个或多个位组成的标志的位字段。

如果 value 等于命名枚举常量的组合,则返回这些常量名称的分隔符分隔列表。 value 搜索标志,从值为最大值的标志转到最小值。 对于对应于位字段 value的每个标志,常量的名称将连接到分隔符分隔的列表。 然后,该标志的值被排除在进一步考虑之外,搜索会继续搜索下一个标志。

如果 value 与命名枚举常量的组合不相等,则返回等效的小 value 数。
“X”或“x” value 不带前导“0x”的十六进制格式表示。
“D”或“d” value 十进制形式表示。
“F”或“f” 行为与“G”或“g”相同,只是 FlagsAttribute 不需要在 Enum 声明中存在。

适用于

另请参阅