为什么我必须在 C# 中将枚举强制转换为 int?

发布于 2024-07-08 22:17:54 字数 626 浏览 8 评论 0原文

这是我的代码:

internal enum WindowsMessagesFlags {
    WM_EXITSIZEMOVE      = 0x00000232,
    WM_DISPLAYCHANGE     = 0x0000007e,
    WM_MOVING            = 0x00000216,
}

protected override void WndProc(ref Message m) {
    switch(m.Msg) {
        case (int)WindowsMessagesFlags.WM_DISPLAYCHANGE:
            FixWindowSnapping();
            break;
        case (int)WindowsMessagesFlags.WM_EXITSIZEMOVE:
            SaveWindowProperties();
            break;
        case (int)WindowsMessagesFlags.WM_MOVING:
            KeepProperLocation(ref m);
            break;
    }
}

有什么办法可以阻止强制转换吗?

This is my code:

internal enum WindowsMessagesFlags {
    WM_EXITSIZEMOVE      = 0x00000232,
    WM_DISPLAYCHANGE     = 0x0000007e,
    WM_MOVING            = 0x00000216,
}

protected override void WndProc(ref Message m) {
    switch(m.Msg) {
        case (int)WindowsMessagesFlags.WM_DISPLAYCHANGE:
            FixWindowSnapping();
            break;
        case (int)WindowsMessagesFlags.WM_EXITSIZEMOVE:
            SaveWindowProperties();
            break;
        case (int)WindowsMessagesFlags.WM_MOVING:
            KeepProperLocation(ref m);
            break;
    }
}

Is there anyway to prevent the casting?

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

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

发布评论

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

评论(3

落墨 2024-07-15 22:17:54

有点 - 强制转换 m.Msg:

protected override void WndProc(ref Message m) {
    switch((WindowsMessagesFlags) m.Msg) {
        case WindowsMessagesFlags.WM_DISPLAYCHANGE:
                FixWindowSnapping();
                break;
        case WindowsMessagesFlags.WM_EXITSIZEMOVE:
                SaveWindowProperties();
                break;
        case WindowsMessagesFlags.WM_MOVING:
                KeepProperLocation(ref m);
                break;
    }
}

您需要强制转换的原因是因为在 C# 中,枚举不仅仅是数字 - 它们是与类型关联的数字。 这会阻止您执行以下操作(无需强制转换):

HttpStatusCode status = someWindowsMessageFlag;

这显然是一件好事:)但是,当您需要时,您始终可以“通过”基础类型(在本例中为 int )。

Sort of - cast m.Msg instead:

protected override void WndProc(ref Message m) {
    switch((WindowsMessagesFlags) m.Msg) {
        case WindowsMessagesFlags.WM_DISPLAYCHANGE:
                FixWindowSnapping();
                break;
        case WindowsMessagesFlags.WM_EXITSIZEMOVE:
                SaveWindowProperties();
                break;
        case WindowsMessagesFlags.WM_MOVING:
                KeepProperLocation(ref m);
                break;
    }
}

The reason you need the cast at all is because in C# enums aren't just numbers - they're numbers associated with the type. This prevents you from doing (without casting):

HttpStatusCode status = someWindowsMessageFlag;

This is clearly a good thing :) When you need to, however, you can always go "via" the underlying type (int in this case).

断肠人 2024-07-15 22:17:54

Message.Msg 定义是什么?

我打赌它是 Int32。

我还打赌 WindowsMessagesFlags 是你的类型,但 Message 来自框架。

这意味着您将自己的枚举与框架构建的对象一起使用,当然它们在类型方面会有一些不兼容性。

枚举是一种强类型,不仅仅是数字,它是在上下文中带有名称的数字。 这个名称、上下文、数字、部分并不直接与数字兼容,这就是您需要进行转换的原因。

What is Message.Msg defined as?

I'm betting it is Int32.

I'm also betting WindowsMessagesFlags is your type, but Message is from the framework.

Which means you're using your own enum with a framework-built object, and of course they are going to have some incompatibilities regarding types.

An enum is a strong type, not just a number, it is a number with a name in a context. This name, context, number, part isn't directly compatible with just numbers, and that's why you need to cast.

抚你发端 2024-07-15 22:17:54

原因之一是 C# 目前 (4.0) 不允许您在扩展方法中编写隐式运算符重载(强制转换)(相关问题)适用于包括枚举在内的任何类型。 例如,在与弱类型数据库或弱类型序列化格式(二进制编写器)集成时,能够干净地与 Int16 进行转换。

One reason is because C# currently (4.0) doesn't allow you to write an implicit operator overload (a cast) within an extension method (related question) for any type including an enumeration. It would be nice to cleanly convert to/from Int16 when integrating with a weakly typed database for instance or a weakly typed serialization format (binary writer).

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