当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Type.GetEnumNames()用法及代码示例


Type.GetEnumNames()方法用于返回当前枚举类型的成员的名称。

用法:

public virtual string[] GetEnumNames ();

返回值:此方法返回一个包含枚举成员名称的数组。


异常:如果当前类型不是枚举,则此方法将提供ArgumentException。

以下示例程序旨在说明上述方法的用法:

示例1:

// C# program to demonstrate the 
// Type.GetEnumNames(Object) Method 
using System; 
using System.Globalization; 
using System.Reflection; 
  
class GFG { 
  
    // Defining enum ABC 
    enum ABC { A, 
               B, 
               C, 
               D, 
               E, 
               F } 
  
    // Main Method 
    public static void Main() 
    { 
  
        // 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(); 
  
            // Getting Array of constants 
            // By using GetEnumNames() method 
            string[] obj = type.GetEnumNames(); 
  
            // Display Array obj 
            Console.WriteLine("The constants are as follow :-"); 
            for (int i = 0; i < obj.Length; i++) 
                Console.Write("{0} ", obj[i]); 
        } 
  
        // catch ArgumentException here 
        catch (ArgumentException e) 
        { 
            Console.Write("The current type is not an enumeration."); 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
输出:
The constants are as follow :-
A B C D E F

示例2:对于ArgumentException

// C# program to demonstrate the 
// Type.GetEnumNames(Object) Method 
using System; 
using System.Globalization; 
using System.Reflection; 
  
class GFG { 
  
    // Defining enum ABC 
    enum ABC { A, 
               B, 
               C, 
               D, 
               E, 
               F } 
  
    // Main Method 
    public static void Main() 
    { 
  
        // Creating try-catch block 
        // to handle exceptions 
        try { 
  
            // Declaring and initializing 
            // object of Type 
            Type type = typeof(int); 
  
            // Getting Array of constants 
            // By using GetEnumNames() method 
            string[] obj = type.GetEnumNames(); 
  
            // Display Array obj 
            Console.WriteLine("The constants are as follow :-"); 
            for (int i = 0; i < obj.Length; i++) 
                Console.Write("{0} ", obj[i]); 
        } 
  
        // 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

参考:



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 C# | Type.GetEnumNames() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。