如何从 System.Enum 转换为基本整数?
我想创建一个通用方法,用于将任何 System.Enum 派生类型转换为其相应的整数值,无需进行强制转换,最好无需解析字符串。
例如,我想要的是这样的:
// Trivial example, not actually what I'm doing.
class Converter
{
int ToInteger(System.Enum anEnum)
{
(int)anEnum;
}
}
但这似乎不起作用。 Resharper 报告您无法将“System.Enum”类型的表达式转换为“int”类型。
现在我已经提出了这个解决方案,但我宁愿有更有效的方法。
class Converter
{
int ToInteger(System.Enum anEnum)
{
return int.Parse(anEnum.ToString("d"));
}
}
有什么建议么?
I'd like to create a generic method for converting any System.Enum derived type to its corresponding integer value, without casting and preferably without parsing a string.
Eg, what I want is something like this:
// Trivial example, not actually what I'm doing.
class Converter
{
int ToInteger(System.Enum anEnum)
{
(int)anEnum;
}
}
But this doesn't appear to work. Resharper reports that you can not cast expression of type 'System.Enum' to type 'int'.
Now I've come up with this solution but I'd rather have something more efficient.
class Converter
{
int ToInteger(System.Enum anEnum)
{
return int.Parse(anEnum.ToString("d"));
}
}
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果你不想施放,
也可以这样做。
直接转换(通过
(int)enumValue
)是不可能的。 请注意,这也是“危险的”,因为枚举可以具有不同的基础类型(int
、long
、byte
...)。更正式地说:
System.Enum
与Int32
没有直接继承关系(尽管两者都是ValueType
),因此显式转换在以下范围内无法正确类型系统If you don't want to cast,
could do the trick.
The direct cast (via
(int)enumValue
) is not possible. Note that this would also be "dangerous" since an enum can have different underlying types (int
,long
,byte
...).More formally:
System.Enum
has no direct inheritance relationship withInt32
(though both areValueType
s), so the explicit cast cannot be correct within the type system我通过先转换为对象然后转换为 int 来使其工作:
这很丑陋,而且可能不是最好的方法。 我会继续搞乱它,看看我是否能想出更好的东西......
编辑:正要发布 Convert.ToInt32(enumValue) 也可以工作,并注意到 MartinStettner 打败了我。
测试:
编辑2:在评论中,有人说这只适用于C# 3.0。 我刚刚在 VS2005 中测试了这个,效果如下:
I got it to work by casting to an object and then to an int:
This is ugly and probably not the best way. I'll keep messing with it, to see if I can come up with something better....
EDIT: Was just about to post that Convert.ToInt32(enumValue) works as well, and noticed that MartinStettner beat me to it.
Test:
EDIT 2: In the comments, someone said that this only works in C# 3.0. I just tested this in VS2005 like this and it worked:
如果您需要将 any 枚举转换为其基础类型(并非所有枚举都由
int
支持),那么您可以使用:If you need to convert any enum to its underlying type (not all enums are backed by
int
) then you can use:为什么需要使用辅助方法重新发明轮子? 将
enum
值转换为其基础类型是完全合法的。它的打字量更少,而且在我看来更具可读性,使用...
...而不是类似...
Why do you need to reinvent the wheel with a helper method? It's perfectly legal to cast an
enum
value to its underlying type.It's less typing, and in my opinion more readable, to use...
...rather than something like...
从我的回答这里:
给定
e
如下:那么这些工作:
最后两个是简单的丑陋的。 第一个应该更具可读性,尽管第二个要快得多。 或者可能是扩展方法是最好的,两全其美。
现在您可以致电:
From my answer here:
Given
e
as in:Then these work:
The last two are plain ugly. The first one should be more readable, though the second one is much faster. Or may be an extension method is the best, best of both worlds.
Now you can call:
从
System.Enum
转换为int
对我来说效果很好(它也在 MSDN)。 也许这是 Resharper 的错误。Casting from a
System.Enum
to anint
works fine for me (it's also on the MSDN). Perhaps it's a Resharper bug.由于枚举仅限于 byte、sbyte、short、ushort、int、uint、long 和 ulong,因此我们可以做出一些假设。
我们可以通过使用最大的可用容器来避免转换过程中的异常。 不幸的是,使用哪个容器并不清楚,因为 ulong 会因负数而爆炸,而 long 会因 long.MaxValue 和 ulong.MaxValue 之间的数字而爆炸。 我们需要根据底层类型在这些选择之间进行切换。
当然,您仍然需要决定当结果不适合 int 时该怎么做。 我认为强制转换是可以的,但仍然存在一些问题:
这是我的建议,我将让读者决定在哪里公开这个函数; 作为帮助者或扩展。
Since Enums are restricted to byte, sbyte, short, ushort, int, uint, long and ulong, we can make some assumptions.
We can avoid exceptions during conversion by using the largest available container. Unfortunately, which container to use is not clear because ulong will blow up for negative numbers and long will blow up for numbers between long.MaxValue and ulong.MaxValue. We need to switch between these choices based on the underlying type.
Of course, you still need to decide what to do when the result doesn't fit inside an int. I think casting is okay, but there are still some gotchas:
Here's my suggestion, I'll leave it to the reader to decide where to expose this function; as a helper or an extension.
如果您阅读
Convert.ToInt32
的代码,您可以看到它将Enum
转换为IConvertible
,然后调用ToInt32(null )
。If you read the code for
Convert.ToInt32
, you can see that it casts theEnum
toIConvertible
, then callsToInt32(null)
.不要忘记 Enum 类型本身有一堆静态辅助函数。 如果您只想将枚举的实例转换为其相应的整数类型,那么强制转换可能是最有效的方法。
我认为 ReSharper 正在抱怨,因为 Enum 不是任何特定类型的枚举,并且枚举本身派生自标量值类型,而不是 Enum。 如果您需要以通用方式进行适应性转换,我想说这可以很好地适合您(请注意,枚举类型本身也包含在通用中:
然后可以像这样使用:
我不能肯定地说我的想法,但上面的代码甚至可能得到 C# 类型推断的支持,允许以下内容:
Don't forget that the Enum type itself has a bunch of static helper functions in it. If all you want to do is convert an instance of the enum to its corresponding integer type, then casting is probably the most efficient way.
I think ReSharper is complaining because Enum isn't an enumeration of any particular type, and enumerations themselves derive from a scalar valuetype, not Enum. If you need adaptable casting in a generic way, I would say this could suite you well (note that the enumeration type itself is also included in the generic:
This could then be used like so:
I can't say for sure off the top of my head, but the above code might even be supported by C#'s type inference, allowing the following: