Enum.GetName 方法

定义

重载

名称 说明
GetName(Type, Object)

检索具有指定值的指定枚举中的常量的名称。

GetName<TEnum>(TEnum)

检索具有指定值的指定枚举类型中的常量的名称。

GetName(Type, Object)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

检索具有指定值的指定枚举中的常量的名称。

public:
 static System::String ^ GetName(Type ^ enumType, System::Object ^ value);
public static string GetName(Type enumType, object value);
public static string? GetName(Type enumType, object value);
[System.Runtime.InteropServices.ComVisible(true)]
public static string GetName(Type enumType, object value);
static member GetName : Type * obj -> string
[<System.Runtime.InteropServices.ComVisible(true)>]
static member GetName : Type * obj -> string
Public Shared Function GetName (enumType As Type, value As Object) As String

参数

enumType
Type

枚举类型。

value
Object

特定枚举常量在其基础类型方面的值。

返回

一个字符串,其中包含其值为enumType枚举常量value的名称;如果未null找到此类常量,

属性

例外

enumTypevaluenull.

enumType不是 .Enum

-或-

value 既不是类型 enumType ,也不具有与 enumType该类型相同的基础类型。

.NET 8 及更高版本:enumType 是布尔支持的枚举类型。

示例

下面的示例演示了 GetName如何使用 。

using System;

public class GetNameTest {
    enum Colors { Red, Green, Blue, Yellow };
    enum Styles { Plaid, Striped, Tartan, Corduroy };

    public static void Main() {

        Console.WriteLine("The 4th value of the Colors Enum is {0}", Enum.GetName(typeof(Colors), 3));
        Console.WriteLine("The 4th value of the Styles Enum is {0}", Enum.GetName(typeof(Styles), 3));
    }
}
// The example displays the following output:
//       The 4th value of the Colors Enum is Yellow
//       The 4th value of the Styles Enum is Corduroy
open System

type Colors =
    | Red = 0
    | Green = 1
    | Blue = 2
    | Yellow = 3

type Styles =
    | Plaid = 0
    | Striped = 1
    | Tartan = 2
    | Corduroy = 3

printfn $"The 4th value of the Colors Enum is {Enum.GetName(typeof<Colors>, 3)}"
printfn $"The 4th value of the Styles Enum is {Enum.GetName(typeof<Styles>, 3)}"
// The example displays the following output:
//       The 4th value of the Colors Enum is Yellow
//       The 4th value of the Styles Enum is Corduroy
Public Class GetNameTest
    
    Enum Colors
        Red
        Green
        Blue
        Yellow
    End Enum 'Colors
    
    Enum Styles
        Plaid
        Striped
        Tartan
        Corduroy
    End Enum 'Styles
    
    Public Shared Sub Main() 
        Console.WriteLine("The 4th value of the Colors Enum is {0}", [Enum].GetName(GetType(Colors), 3))
        Console.WriteLine("The 4th value of the Styles Enum is {0}", [Enum].GetName(GetType(Styles), 3))
    End Sub
End Class
' The example displays the following output:
'       The 4th value of the Colors Enum is Yellow
'       The 4th value of the Styles Enum is Corduroy

注解

如果多个枚举成员具有相同的基础值,该方法 GetName 将保证它将返回其中一个枚举成员的名称。 但是,它不保证它始终返回同一枚举成员的名称。 因此,当多个枚举成员具有相同的值时,应用程序代码不应依赖于返回特定成员名称的方法。

适用于

GetName<TEnum>(TEnum)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

检索具有指定值的指定枚举类型中的常量的名称。

public:
generic <typename TEnum>
 where TEnum : value class static System::String ^ GetName(TEnum value);
public static string? GetName<TEnum>(TEnum value) where TEnum : struct;
static member GetName : 'Enum -> string (requires 'Enum : struct)
Public Shared Function GetName(Of TEnum As Structure) (value As TEnum) As String

类型参数

TEnum

枚举的类型。

参数

value
TEnum

特定枚举常量在其基础类型方面的值。

返回

一个字符串,其中包含其值为TEnum枚举常量value的名称;如果未null找到此类常量,

例外

.NET 8 及更高版本:TEnum 是布尔支持的枚举类型。

适用于