Enum.GetName 方法
在指定枚举中检索具有指定值的常数的名称。
**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
<ComVisibleAttribute(True)> _
Public Shared Function GetName ( _
enumType As Type, _
value As Object _
) As String
用法
Dim enumType As Type
Dim value As Object
Dim returnValue As String
returnValue = Enum.GetName(enumType, value)
[ComVisibleAttribute(true)]
public static string GetName (
Type enumType,
Object value
)
[ComVisibleAttribute(true)]
public:
static String^ GetName (
Type^ enumType,
Object^ value
)
/** @attribute ComVisibleAttribute(true) */
public static String GetName (
Type enumType,
Object value
)
ComVisibleAttribute(true)
public static function GetName (
enumType : Type,
value : Object
) : String
参数
- enumType
枚举类型。
- value
特定枚举常数的值(根据其基础类型)。
返回值
一个字符串,该字符串包含 enumType 的枚举常数的名称,该常数的值为 value;或者,如果没有找到这样的常数,则为 空引用(在 Visual Basic 中为 Nothing)。
异常
异常类型 | 条件 |
---|---|
enumType 或 value 为 空引用(在 Visual Basic 中为 Nothing)。 |
|
enumType 不是 Enum。 - 或 - value 既不是 enumType 类型,也没有与 enumType 相同的基础类型。 |
示例
下面的代码示例说明如何使用 GetName:
Imports System
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 'Main
End Class 'GetNameTest
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));
}
}
using namespace System;
enum class Colors
{
Red, Green, Blue, Yellow
};
enum class Styles
{
Plaid, Striped, Tartan, Corduroy
};
int main()
{
Console::WriteLine( "The 4th value of the Colors Enum is {0}", Enum::GetName( Colors::typeid, 3 ) );
Console::WriteLine( "The 4th value of the Styles Enum is {0}", Enum::GetName( Styles::typeid, 3 ) );
}
import System.*;
public class GetNameTest
{
enum Colors
{
red (0),
green (1),
blue (2),
yellow (3);
}//Colors
enum Styles
{
plaid (0),
striped (1),
tartan (2),
corduroy (3);
}//Styles
public static void main(String[] args)
{
Console.WriteLine("The 4th value of the Colors Enum is {0}",
Enum.GetName(Colors.class.ToType(), (Int32)3));
Console.WriteLine("The 4th value of the Styles Enum is {0}",
Enum.GetName(Styles.class.ToType(), (Int32)3));
} //main
} //GetNameTest
import System;
public class GetNameTest {
enum Colors { Red, Green, Blue, Yellow };
enum Styles { Plaid, Striped, Tartan, Corduroy };
public static function Main() {
Console.WriteLine("The 4th value of the Colors Enum is {0}", Enum.GetName(Colors, 3));
Console.WriteLine("The 4th value of the Styles Enum is {0}", Enum.GetName(Styles, 3));
}
}
平台
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