Type.GetEnumName(Object)方法用于返回具有当前枚举类型的指定值的常量的名称。
句法:
public virtual string GetEnumName (object value);
在这里,它使用要检索其名称的值。
返回值:该方法返回具有指定值的当前枚举类型的成员的名称。如果找不到这样的常量,则它将返回null。
例外情况:
- ArgumentException :如果当前类型不是枚举,或者值既不是当前类型,也不具有与当前类型相同的基础类型。
- ArgumentNullException :如果value为null。
下面的程序说明了上述方法的用法:
范例1:
// C# program to demonstrate the
// Type.GetEnumName(Object) Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Defining enum ABC
enum ABC {A, B, C}
// Main Method
public static void Main()
{
// Creating try-catch block
// to handle exceptions
try {
// Creating and initializing object
// of ABC with instace of enum ABC
ABC a = ABC.A;
// Declaring and initializing
// object of Type
Type type = a.GetType();
string obj = type.GetEnumName(a);
Console.WriteLine("Name of constant is: " + obj);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
输出:
Name of constant is: A
示例2:对于ArgumentException
// C# program to demonstrate the
// Type.GetEnumName(Object) Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Defining enum ABC
enum ABC {A, B, C}
// Main Method
public static void Main()
{
// Creating try-catch block
// to handle exceptions
try {
// Creating and initializing object
// of ABC with instace of enum ABC
ABC a = ABC.A;
// Declaring and initializing
// object of Type
Type type = typeof(int);
string obj = type.GetEnumName(a);
Console.WriteLine("Name of constant is: " + obj);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.WriteLine("value is null. ");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
// catch ArgumentException here
catch (ArgumentException e)
{
Console.WriteLine("The current type is not an Enumeration.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
输出:
The current type is not an Enumeration.
Exception Thrown: System.ArgumentException
示例3:对于ArgumentNullException
// C# program to demonstrate the
// Type.GetEnumName(Object) Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Defining enum ABC
enum ABC {A, B, C}
// Main Method
public static void Main()
{
// Creating try-catch block
// to handle exceptions
try {
// Creating and initializing object
// of ABC with instace of enum ABC
ABC a = ABC.A;
// Declaring and initializing
// object of Type
Type type = typeof(int);
string obj = type.GetEnumName(null);
Console.WriteLine("Name of constant is " + obj);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.WriteLine("value is null. ");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
// catch ArgumentException here
catch (ArgumentException e)
{
Console.WriteLine("The current type is not an Enumeration.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
输出:
value is null.
Exception Thrown: System.ArgumentNullException
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.type.getenumname?view=netframework-4.8