简单的十六进制值转换为Enum类型值问题

发布于 2024-11-19 13:06:24 字数 803 浏览 4 评论 0原文

我已经和这个小问题斗争了一晚上了,但我无法解决。我想要的只是拥有一些具有十六进制值的枚举元素并将值转换为枚举,但 .NET 不知何故认为我的枚举不合法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class CommandEnum
    {
        [Flags]
        public enum Command : byte
        {
            CMD_HAND_SHAKE = 0x0,
            CMD_SET_MAX_CURRENT = 0x1,
            CMD_SET_TEST_SPEED = 0xA
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            CommandEnum command;
            byte receiveByte = 0x0A;

            // Error: Type provided must be an Enum. Parameter name: enumType:
            command = (CommandEnum)Enum.ToObject(typeof(CommandEnum), receiveByte);  
        }
    }
}

出了什么问题,我应该如何解决?

I have been fighting with this little problem for a night and I couldn't figure it out. All I want is have some enum elements with Hex value and convert a value to enum, but .NET somehow don't think my enum is legal.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class CommandEnum
    {
        [Flags]
        public enum Command : byte
        {
            CMD_HAND_SHAKE = 0x0,
            CMD_SET_MAX_CURRENT = 0x1,
            CMD_SET_TEST_SPEED = 0xA
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            CommandEnum command;
            byte receiveByte = 0x0A;

            // Error: Type provided must be an Enum. Parameter name: enumType:
            command = (CommandEnum)Enum.ToObject(typeof(CommandEnum), receiveByte);  
        }
    }
}

What is wrong and how should I fix it?

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

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

发布评论

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

评论(5

左耳近心 2024-11-26 13:06:24

您应该使用

command = (CommandEnum.Command)receiveByte;

并且command应该定义为

ConnamdEnum.Command command

通常,看起来您不需要包装类CommandEnum,您应该只定义enum Command

You should use

command = (CommandEnum.Command)receiveByte;

and command should be defined as

ConnamdEnum.Command command

Generally, it looks like you do not need a wrapper class CommandEnum, you should just define enum Command

甜扑 2024-11-26 13:06:24

CommandEnum 不是枚举,它是一个类!您已将枚举 Command 定义为“内部”枚举,因此以下内容应该有效:

command = (CommandEnum.Command)Enum.ToObject(typeof(CommandEnum.Command),
                                             receiveByte);

CommandEnum is not an enum, it is a class! You have defined your enum Command as an 'inner' enum, so the following should work:

command = (CommandEnum.Command)Enum.ToObject(typeof(CommandEnum.Command),
                                             receiveByte);
春夜浅 2024-11-26 13:06:24

您向其传递包含枚举 CommandEnum 的类的名称,而不是枚举 CommandEnum.Command 的名称。以下内容应该适合您:

CommandEnum.Command command = (CommandEnum.Command)Enum.ToObject(typeof(CommandEnum.Command), receiveByte);

此外,您使用的语法相当复杂,您可以直接转换:

CommandEnum.Command command1 = (CommandEnum.Command)receiveByte;

You are passing it the name of the class containing your enum CommandEnum, instead of the name of your enum, CommandEnum.Command. The following should work for you:

CommandEnum.Command command = (CommandEnum.Command)Enum.ToObject(typeof(CommandEnum.Command), receiveByte);

Also, the syntax that you are using is fairly convoluted, you can just cast instead:

CommandEnum.Command command1 = (CommandEnum.Command)receiveByte;
单身狗的梦 2024-11-26 13:06:24

尝试

CommandEnum.Command command;
byte receiveByte = 0x0A;

// Error: Type provided must be an Enum. Parameter name: enumType:
command = (CommandEnum.Command)Enum.ToObject(typeof(CommandEnum.Command), receiveByte);  

或更好,

command = (CommandEnum.Command)receiveByte;  

在您的原始代码中您使用了所有错误的类型。您的枚举类型是 CommandEnum 类的公共嵌套类型(并且确实没有充分的理由这样做)。尝试摆脱 CommandEnum 类并仅使用 Command 枚举。

Try

CommandEnum.Command command;
byte receiveByte = 0x0A;

// Error: Type provided must be an Enum. Parameter name: enumType:
command = (CommandEnum.Command)Enum.ToObject(typeof(CommandEnum.Command), receiveByte);  

or better,

command = (CommandEnum.Command)receiveByte;  

In your original code you are using all the wrong types. Your enumeration type is a public nested type for CommandEnum class (and there is really no good reason to do something like that). Try to get rid of CommandEnum class and justuse the Command enum.

满天都是小星星 2024-11-26 13:06:24

由于您的枚举是 typeof(byte) 只是将其强制转换,因此无需执行任何其他操作

CommandEnum.Command val= (CommandEnum.Command)receiveByte;

PS 您不能有 0x00 标志。

Since your enum is typeof(byte) just cast it, no need to do anything else

CommandEnum.Command val= (CommandEnum.Command)receiveByte;

P.S. You can't have a 0x00 flag.

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