Share via


Enum.Format 方法

根据指定格式将指定枚举类型的指定值转换为其等效的字符串表示形式。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<ComVisibleAttribute(True)> _
Public Shared Function Format ( _
    enumType As Type, _
    value As Object, _
    format As String _
) As String
用法
Dim enumType As Type
Dim value As Object
Dim format As String
Dim returnValue As String

returnValue = Enum.Format(enumType, value, format)
[ComVisibleAttribute(true)] 
public static string Format (
    Type enumType,
    Object value,
    string format
)
[ComVisibleAttribute(true)] 
public:
static String^ Format (
    Type^ enumType, 
    Object^ value, 
    String^ format
)
/** @attribute ComVisibleAttribute(true) */ 
public static String Format (
    Type enumType, 
    Object value, 
    String format
)
ComVisibleAttribute(true) 
public static function Format (
    enumType : Type, 
    value : Object, 
    format : String
) : String

参数

  • enumType
    要转换的值的枚举类型。
  • value
    要转换的值。
  • format
    要使用的输出格式。

返回值

value 的字符串表示形式。

异常

异常类型 条件

ArgumentNullException

enumType、value 或 format 参数为 空引用(在 Visual Basic 中为 Nothing)。

ArgumentException

enumType 参数不是 Enum 类型。

- 或 -

value 来自于其类型与 enumType 不同的枚举。

- 或 -

value 的类型不是 enumType 的基础类型。

FormatException

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”执行的行为是相同的,只是在 Enum 声明中不需要 FlagsAttribute

示例

下面的代码示例说明如何在 Enum 上下文中使用 Format

Imports System

Public Class FormatTest
    
    Enum Colors
        Red
        Green
        Blue
        Yellow    
    End Enum 'Colors
    
    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 'Main
End Class 'FormatTest
using System;

public class FormatTest {
    enum Colors { Red, Green, Blue, Yellow };

    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"));
    }
}
using namespace System;
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" ) );
}
import System.*;

public class FormatTest
{
    enum Colors
    {
        red (0),
        green (1),
        blue (2),
        yellow (3);
    } //Colors

    public static void main(String[] args)
    {
        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.class.ToType(), myColor, "d"));
        Console.WriteLine("The hex value of my favorite color is {0}", 
            Enum.Format(Colors.class.ToType(), myColor, "x"));
    } //main
} //FormatTest
import System;

public class FormatTest {
    enum Colors { Red, Green, Blue, Yellow };

    public static function Main() {
        var myColor : Colors = Colors.Blue;

        Console.WriteLine("My favorite color is {0}", myColor);
        Console.WriteLine("The value of my favorite color is {0}", Enum.Format(Colors, myColor, "d"));
        Console.WriteLine("The hex value of my favorite color is {0}", Enum.Format(Colors, myColor, "x"));
    }
}

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

Enum 结构
Enum 成员
System 命名空间
ToString

其他资源

格式化概述
枚举格式字符串