[英]How do you serialize and deserialize an enum?
I am trying to create an enum with commands for motor control in VB.NET. 我试图用VB.NET中的电机控制命令创建一个枚举。 I want to set the command on one computer, serialize it, send it to another computer over a TCP connection, deserialize, and interpret the command.
我想在一台计算机上设置命令,将其序列化,通过TCP连接将其发送到另一台计算机,反序列化并解释该命令。 I know how to use the TCP connection, but I'm missing conceptual knowledge about the enum.
我知道如何使用TCP连接,但是我缺少有关枚举的概念性知识。 I am using Protobuf-net to serialize and have the following description of commands.
我正在使用Protobuf-net进行序列化,并具有以下命令描述。
Public Class RemoteControl
<ProtoContract>
Public Class Command
<ProtoContract>
Enum CommandAction
<ProtoMember(2)>
HOME_MOTOR
<ProtoMember(1)>
MOVE_ABS
End Enum
End Class
End Class
My question is, how do I set the instance of a RemoteControl
object to the action I want? 我的问题是,如何将
RemoteControl
对象的实例设置为所需的操作? I know enums use integers, so to send a MOVE_ABS
(which has a tag of 1), I tried 我知道枚举使用整数,因此要发送
MOVE_ABS
(标记为1),我尝试了
Dim myAction As New RemoteControl
myAction.Command.CommandAction = 1
This returned an error saying "CommandAction is a type and cannot be used as an expression". 这返回一个错误,指出“ CommandAction是类型,不能用作表达式”。
Also, once I do manage to figure out how to send this command, how would I interpret it on the other computer? 另外,一旦我设法弄清楚如何发送该命令,我将如何在另一台计算机上解释它? Would the deserialized value of something like
RemoteControl.Command.CommandAction
be equal to 1 if the command sent was MOVE_ABS
? 如果发送的命令是
MOVE_ABS
则RemoteControl.Command.CommandAction
之类的反序列化值是否等于1?
As usual, I spent days on this then figured it out immediately after making this post. 像往常一样,我花了几天时间在发布这篇文章后立即弄清楚了。
What I ended up doing was: 我最终要做的是:
SERVER SIDE
Dim realProto As New RemoteControl.Command.CommandAction
realProto = RemoteControl.Command.CommandAction.MOVE_ABS
ProtoBuf.Serializer.SerializeWithLengthPrefix(myStream, realProto, ProtoBuf.PrefixStyle.Fixed32)
CLIENT SIDE
Dim readProto As New RemoteControl.Command.CommandAction
readProto = Protobuf.Serializer.DeserializeWithLengthPrefix(Of RemoteControl.Command.CommandAction)(myStream, Protobuf.PrefixStyle.Fixed32)
This then sets readProto
as a RemoteControl.Command.CommandAction
whose value is an integer corresponding to the action tag, or I can do readProto.ToString
to view the name of the action. 然后,将
readProto
设置为RemoteControl.Command.CommandAction
其值为对应于操作标记的整数,或者我可以执行readProto.ToString
来查看操作的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.