将 Enum 转换为 uint

发布于 2024-08-13 20:37:49 字数 810 浏览 4 评论 0原文

我正在编写一个函数,它接受 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 技术交流群。

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

发布评论

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

评论(3

玩世 2024-08-20 20:37:49

(uint) (object) myEnum 方法失败,因为默认情况下,C# 枚举使用 int 作为其基础类型,而 int 就是当它们被装箱时就会变成这样。 C# 语法使其看起来像是从其基础类型继承的枚举(例如 enum MyEnum : uint)。

您必须明确告诉编译器首先从 object 取消装箱到 int,然后进行从 intuint 的数值转换代码>. (尽管语法相同,但从 object 拆箱为值类型与 intuint 之间的转换是不同的过程。)

The (uint) (object) myEnum approach fails because, by default, C# enums use int as their underlying type, and an int is what they get turned into when they're boxed. The C# syntax makes it look like enums inherit from their underlying type (like enum MyEnum : uint).

You have to explicitly tell the compiler to unbox from object to int first, then do a numerical conversion from int to uint. (Even though the syntax is the same, unboxing from object to a value type is a different process from casting between int and uint.)

岁月静好 2024-08-20 20:37:49

根据我在转换为 int 时所看到的情况,您必须首先将其转换为对象:(int) (object) myEnum。如果你写 (int) myEnum 你会得到一个编译时异常。

不正确。您可以直接将枚举值转换为整数类型。

现在,当我尝试将其转换为 uint 时,我期望 (uint) (object) myEnum 没问题。它编译得很好,但运行时会生成 InvalidCastException。为了让它发挥作用,我使用了:
(uint) (int) (object) myEnum
我觉得这看起来很有趣,所以我很高兴,但是为什么会这样呢?

当您将枚举值转换为 object 时,它将被装箱为其底层类型,默认情况下为 int。您无法直接将装箱值取消装箱为实际类型以外的任何类型。即使这样也会失败:

short s = 10;
object o = s;
int i = (int)o; // throws `InvalidCastException` while `int i = (int)s;` works.

From what I've seen when casting to int, you have to cast it to an object first: (int) (object) myEnum. If you write (int) myEnum you get a compile time exception.

Not true. You can directly cast an enum value to an integer type.

Now, when I tried to cast it to uint, I was expecting that (uint) (object) myEnum 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) myEnum
I think it looks funny, so I'm quite happy, but why is it so?

When you cast an enum value to object, it'll be boxed as its underlying type, which is int by default. You cannot unbox a boxed value to any type other than its actual type directly. Even this will fail:

short s = 10;
object o = s;
int i = (int)o; // throws `InvalidCastException` while `int i = (int)s;` works.
欲拥i 2024-08-20 20:37:49

如果您需要朝相反的方向前进,即从基础类型到特定类型的枚举:

enum MyEnumType { a, b, c };
//...
MyEnumType enum_val = (MyEnumType)Enum.ToObject(typeof(MyEnumType), 2);
Console.WriteLine(enum_val == MyEnumType.c);
// "True"

我发现在创作泛型类型时这是必要的(而不是简单的转换)。由于您无法使用“where”子句来充分约束通用;对于您的枚举类型,编译器不会让您将数字转换为它。

In case you need to go in the opposite direction, that is, from an underlying type to the enumeration of a particular type:

enum MyEnumType { a, b, c };
//...
MyEnumType enum_val = (MyEnumType)Enum.ToObject(typeof(MyEnumType), 2);
Console.WriteLine(enum_val == MyEnumType.c);
// "True"

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.

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