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
參考:
相關用法
- C# DateTimeOffset.Add()用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# Math.Sin()用法及代碼示例
- C# Math.Cos()用法及代碼示例
- C# Dictionary.Add()用法及代碼示例
- C# Math.Tan()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# Math.Exp()用法及代碼示例
- C# Math.Abs()函數用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Type.GetEnumName() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。