将 Enum 转换为 uint
我正在编写一个函数,它接受 Enum
并将其转换为 uint
。根据我在转换为 int
时所见,您必须首先将其转换为对象:(int) (object) myEnumValue
。如果你写 (int) myEnumValue
你会得到一个编译时异常。
现在,当我尝试将其转换为 uint 时,我期望 (uint) (object) myEnumValue
会没问题。它编译得很好,但运行时,它会生成一个 InvalidCastException
。为了让它发挥作用,我用了
(uint) (int) (object) myEnumValue
我认为它看起来很有趣,所以我很高兴,但为什么会这样呢?
也许问为什么不可能将 object
转换为 uint
会更正确,但我感兴趣的是是否还有另一种方法可以从 <代码>枚举到<代码>uint。有没有?
编辑:
上下文是一个函数,如下所示:
public static uint ToUInt (Enum e)
{
return (uint) (int) (object) e;
}
编辑2:
最好的解决方案是thecoop提到的:
Convert.ToUInt32(e)
I'm writing a function that takes an Enum
and casts it to uint
. From what I've seen when casting to int
, you have to cast it to an object first: (int) (object) myEnumValue
. If you write (int) myEnumValue
you get a compile time exception.
Now, when I tried to cast it to uint, I was expecting that (uint) (object) myEnumValue
would be alright. It compiles nicely, but when run, it generates an InvalidCastException
. So to get it to work, I've used
(uint) (int) (object) myEnumValue
I think it looks funny, so I'm quite happy, but why is it so?
Maybe it would have been more correct to ask why it is not possible to cast object
to uint
, but I'm interested in whether there is another way to go from an Enum
to uint
. Is there?
Edit:
The context is a function, something like this:
public static uint ToUInt (Enum e)
{
return (uint) (int) (object) e;
}
Edit 2:
The best solution was as mentioned by thecoop:
Convert.ToUInt32(e)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(uint) (object) myEnum
方法失败,因为默认情况下,C# 枚举使用int
作为其基础类型,而int
就是当它们被装箱时就会变成这样。 C# 语法使其看起来像是从其基础类型继承的枚举(例如enum MyEnum : uint
)。您必须明确告诉编译器首先从
object
取消装箱到int
,然后进行从int
到uint
的数值转换代码>. (尽管语法相同,但从object
拆箱为值类型与int
和uint
之间的转换是不同的过程。)The
(uint) (object) myEnum
approach fails because, by default, C# enums useint
as their underlying type, and anint
is what they get turned into when they're boxed. The C# syntax makes it look like enums inherit from their underlying type (likeenum MyEnum : uint
).You have to explicitly tell the compiler to unbox from
object
toint
first, then do a numerical conversion fromint
touint
. (Even though the syntax is the same, unboxing fromobject
to a value type is a different process from casting betweenint
anduint
.)不正确。您可以直接将枚举值转换为整数类型。
当您将枚举值转换为
object
时,它将被装箱为其底层类型,默认情况下为int
。您无法直接将装箱值取消装箱为实际类型以外的任何类型。即使这样也会失败:Not true. You can directly cast an enum value to an integer type.
When you cast an enum value to
object
, it'll be boxed as its underlying type, which isint
by default. You cannot unbox a boxed value to any type other than its actual type directly. Even this will fail:如果您需要朝相反的方向前进,即从基础类型到特定类型的枚举:
我发现在创作泛型类型时这是必要的(而不是简单的转换)。由于您无法使用“where”子句来充分约束通用;对于您的枚举类型,编译器不会让您将数字转换为它。
In case you need to go in the opposite direction, that is, from an underlying type to the enumeration of a particular type:
I found this to be necessary (rather than simple casting) when authoring generic types. Since you can't use 'where' clauses to sufficiently constrain the generic <T> to your enumeration type, the compiler won't let you cast a numeric to it.