Enum.GetValues() Return Type

后端 未结 6 601
孤独总比滥情好
孤独总比滥情好 2020-12-01 11:45

I have read documentation that states that ‘given the type of the enum, the GetValues() method of System.Enum will return an array of the given enum\'s base type’ ie int, by

相关标签:
6条回答
  • As Roger mentioned in a comment, it would be nice if there was a Enum.GetValues<MyEnum>() generic implementation, but there is not.

    This problem annoyed the heck out of me, as well, so I created a library in C++/CLI that has generic implementations of all of the static methods on the Enum class (as well as a bunch of other generic methods for working with enums).

    The library is written in C++/CLI because C# does not support constraining a generic type by System.Enum. C++/CLI (and the CLR) do support constraining by System.Enum and C#/VB.NET has no problem understanding calls to a method that have this constraint.

    In the case of your example, you'd use Enums.GetValues<MyEnumType>() which will hand you an array of MyEnumType without the need to cast. Though C# and VB.Net do not support defining an enum constraint, they have no problem with consuming a method/class that has such a constraint and intellisense/the compiler handle it perfectly.

    0 讨论(0)
  • 2020-12-01 12:13

    If you're using NET 3.5 (i.e. you have LINQ) you can do:

    var responses = Enum.GetValues(typeof(Response)).Cast<Response>();
    
    0 讨论(0)
  • 2020-12-01 12:13

    Can you please refer to the documentation you mention. The MSDN documentation on Enum.GetValues does not mention anything like that (quote from that page):

    Return Value

    Type: System.Array

    An Array of the values of the constants in enumType. The elements of the array are sorted by the binary values of the enumeration constants.

    0 讨论(0)
  • 2020-12-01 12:23

    Personally I've created a separate method in my Utils project, which I include in my other projects. Here's the code I use:

    public static class EnumUtil
    {
        public static IEnumerable<TEnum> GetAllValues<TEnum>() 
            where TEnum : struct, IConvertible, IComparable, IFormattable
        {
            return Enum.GetValues(typeof(TEnum)).Cast<TEnum>();
        }   
    }
    

    And I call it like this:

    var enumValues = EnumUtil.GetAllValues<Response>();
    
    0 讨论(0)
  • 2020-12-01 12:23

    Similar to Joel's Answer but done a slight different way:

    public static class Enums<T>
      where T : struct, IComparable, IFormattable, IConvertible
    {
      static Enums()
      {
        if (!typeof(T).IsEnum)
          throw new ArgumentException("Type T must be an Enum type");  
      }
    
      public static IEnumerable<T> GetValues()
      {
        var result = ((T[])Enum.GetValues(typeof(T)).ToList()
    
        return result;
      }
    }
    

    Usage:

    IEnumerable<System.Drawing.FontStyle> styles = Enums<System.Drawing.FontStyle>.GetValues();
    
    0 讨论(0)
  • 2020-12-01 12:24

    You need to cast the result to the actual array type you want

    (Response[])Enum.GetValues(typeof(Response))
    

    as GetValues isn't strongly typed

    EDIT: just re-read the answer. You need to explicitly cast each enum value to the underlying type, as GetValues returns an array of the actual enum type rather than the base type. Enum.GetUnderlyingType could help with this.

    0 讨论(0)
提交回复
热议问题