Enum.GetValues() 返回类型
我已阅读文档,其中指出“给定枚举的类型,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要将结果转换为您想要的实际数组类型,
因为 GetValues 不是强类型
编辑:只需重新阅读答案。您需要将每个枚举值显式转换为基础类型,因为 GetValues 返回实际枚举类型而不是基本类型的数组。 Enum.GetUnderlyingType 可以帮助解决这个问题。
You need to cast the result to the actual array type you want
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.
如果您使用 NET 3.5(即您有 LINQ),您可以执行以下操作:
If you're using NET 3.5 (i.e. you have LINQ) you can do:
就我个人而言,我在我的 Utils 项目中创建了一个单独的方法,并将其包含在我的其他项目中。这是我使用的代码:
我这样称呼它:
Personally I've created a separate method in my Utils project, which I include in my other projects. Here's the code I use:
And I call it like this:
您可以参考一下您提到的文档吗? 有关
Enum.GetValues
的文档< /a> 没有提到类似的内容(引自该页面):Can you please refer to the documentation you mention. The documentation on
Enum.GetValues
does not mention anything like that (quote from that page):如果您使用的是 .NET 5+,您现在可以使用
Enum.GetValues()
。https ://learn.microsoft.com/en-us/dotnet/api/system.enum.getvalues?view=net-6.0#system-enum-getvalues-1
If you are using .NET 5+ you can now use
Enum.GetValues<TEnum>()
.https://learn.microsoft.com/en-us/dotnet/api/system.enum.getvalues?view=net-6.0#system-enum-getvalues-1
正如 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 ofMyEnumType
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.类似于 Joel 的回答,但方式略有不同:
用法:
Similar to Joel's Answer but done a slight different way:
Usage: