C# Enum.GetValues() 返回类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1398664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:04:23  来源:igfitidea点击:

Enum.GetValues() Return Type

c#enums

提问by Cragly

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, byte etc

我读过的文档指出“给定枚举的类型,System.Enum 的 GetValues() 方法将返回给定枚举的基本类型的数组”,即 int、byte 等

However I have been using the GetValues method and all I keep getting back is an array of type Enums. Am I missing something??

但是,我一直在使用 GetValues 方法,而我一直返回的是一个枚举类型的数组。我错过了什么吗??


public enum Response
{
    Yes = 1,
    No = 2,
    Maybe = 3
} 

foreach (var value in Enum.GetValues(typeof(Response))) { var type = value.GetType(); // type is always of type Enum not of the enum base type }

Thanks

谢谢

采纳答案by thecoop

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

您需要将结果转换为您想要的实际数组类型

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

as GetValues isn't strongly typed

因为 GetValues 不是强类型的

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.

编辑:只需重新阅读答案。您需要将每个枚举值显式转换为基础类型,因为 GetValues 返回实际枚举类型而不是基类型的数组。Enum.GetUnderlyingType 可以帮助解决这个问题。

回答by Fredrik M?rk

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

您能否参考您提到的文档。上的MSDN 文档Enum.GetValues没有提到类似的内容(引自该页面):

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.

返回值

类型:System.Array

enumType 中常量值的数组。数组元素按枚举常量的二进制值排序。

回答by cdmckay

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

如果您使用 NET 3.5(即您有 LINQ),您可以执行以下操作:

var responses = Enum.GetValues(typeof(Response)).Cast<Response>();

回答by Joel

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

我个人在我的 Utils 项目中创建了一个单独的方法,我将其包含在我的其他项目中。这是我使用的代码:

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>();

回答by mdip

As Rogermentioned in a comment, it would be nice if there was a Enum.GetValues<MyEnum>()generic implementation, but there is not.

正如Roger在评论中提到的,如果有一个Enum.GetValues<MyEnum>()通用的实现会很好,但没有。

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

这个问题也让我很恼火,所以我在 C++/CLI 中创建了一个库,它具有Enum类上所有静态方法的通用实现(以及一堆其他用于处理枚举的通用方法)。

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

该库是用 C++/CLI 编写的,因为 C# 不支持通过 System.Enum 约束泛型类型。C++/CLI(和 CLR)确实支持 System.Enum 的约束,并且 C#/VB.NET 可以毫无问题地理解对具有此约束的方法的调用。

In the case of your example, you'd use Enums.GetValues<MyEnumType>()which will hand you an array of MyEnumTypewithout 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.

在您的示例的情况下,您将使用Enums.GetValues<MyEnumType>()which 将给您一个数组,MyEnumType而无需进行转换。尽管 C# 和 VB.Net 不支持定义枚举约束,但它们在使用具有此类约束的方法/类方面没有问题,并且智能感知/编译器可以完美地处理它。

回答by Erik Philips

Similar to Joel's Answerbut 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();