Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何序列化和反序列化枚举?_Vb.net_Serialization_Enums_Protocol Buffers_Protobuf Net - Fatal编程技术网

Vb.net 如何序列化和反序列化枚举?

Vb.net 如何序列化和反序列化枚举?,vb.net,serialization,enums,protocol-buffers,protobuf-net,Vb.net,Serialization,Enums,Protocol Buffers,Protobuf Net,我正在尝试在VB.NET中创建一个带有电机控制命令的枚举。我想在一台计算机上设置该命令,将其序列化,通过TCP连接将其发送到另一台计算机,反序列化并解释该命令。我知道如何使用TCP连接,但我缺少有关枚举的概念性知识。我正在使用Protobuf net进行序列化,并对命令进行以下描述 Public Class RemoteControl <ProtoContract> Public Class Command <ProtoContract>

我正在尝试在VB.NET中创建一个带有电机控制命令的枚举。我想在一台计算机上设置该命令,将其序列化,通过TCP连接将其发送到另一台计算机,反序列化并解释该命令。我知道如何使用TCP连接,但我缺少有关枚举的概念性知识。我正在使用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
这返回了一个错误,说明CommandAction是一个类型,不能用作表达式


此外,一旦我设法弄清楚如何发送此命令,我将如何在另一台计算机上解释它?如果发送的命令是MOVE\u ABS,那么像RemoteControl.Command.CommandAction这样的反序列化值是否等于1?

像往常一样,我花了几天的时间研究这个问题,然后在写了这篇文章后立即找到了答案

我最后做的是:

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)

然后将readProto设置为RemoteControl.Command.CommandAction,其值为与action标记对应的整数,或者我可以执行readProto.ToString来查看操作的名称。

我过去必须做的是将映射到枚举的属性设置为整数,并在反序列化通过线路发送的数据后将该属性直接强制转换为枚举值。这要求枚举要么位于双方都可以看到的DLL中,要么在通信双方分别声明。也许不是最好的解决方案,但它很有效。谢谢你的建议。我的枚举将在两侧声明,因此我将考虑这一点。
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)