📜  C#| Type.GetEnumValues()方法

📅  最后修改于: 2021-05-29 16:43:33             🧑  作者: Mango

Type.GetEnumValues()方法用于返回当前枚举类型中的常量值的数组。

句法:

public virtual Array GetEnumValues ();

返回值:此方法返回包含值的数组。数组的元素按二进制值(即枚举常量的无符号值)排序。

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

下面的程序说明了上述方法的用法:

范例1:

// C# program to demonstrate the
// Type.GetEnumValues() 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 an array of the values 
            // of the constants
            // By using GetEnumValues() method
            Array obj = type.GetEnumValues();
  
            // Display values of the constants
            Console.Write("Values of the constants is : {0} ", obj);
        }
  
        // 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);
        }
    }
}
输出:
Values of the constants is : GFG+ABC[] 

示例2:对于ArgumentException

// C# program to demonstrate the
// Type.GetEnumValues() 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 = typeof(int);
  
            // Getting an array of the values 
            // of the constants
            // By using GetEnumValues() method
            Array obj = type.GetEnumValues();
  
            // Display values of the constants
            Console.Write("Values of the constants is : {0} ", obj);
        }
  
        // 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

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.type.getenumvalues?view=netframework-4.8