Enum.GetValues() 返回类型

发布于 2024-08-04 02:01:12 字数 427 浏览 2 评论 0原文

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

但是,我一直在使用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
}

I have read the 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" i.e. int, byte, etc.

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

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

著墨染雨君画夕 2024-08-11 02:01:12

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

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

因为 GetValues 不是强类型

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

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.

━╋う一瞬間旳綻放 2024-08-11 02:01:12

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

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

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

var responses = Enum.GetValues(typeof(Response)).Cast<Response>();
思念满溢 2024-08-11 02:01:12

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

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

我这样称呼它:

var enumValues = EnumUtil.GetAllValues<Response>();

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>();
别低头,皇冠会掉 2024-08-11 02:01:12

您可以参考一下您提到的文档吗? 有关 Enum.GetValues 的文档< /a> 没有提到类似的内容(引自该页面):

返回值

类型:System.Array

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

Can you please refer to the documentation you mention. The 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.

荒岛晴空 2024-08-11 02:01:12

正如 Roger 在评论中提到的那样,如果有一个 Enum.GetValues就太好了()通用实现,但是没有。

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

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

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

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.

疑心病 2024-08-11 02:01:12

类似于 Joel 的回答,但方式略有不同:

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;
  }
}

用法:

IEnumerable<System.Drawing.FontStyle> styles = Enums<System.Drawing.FontStyle>.GetValues();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文