Serializer.Serialize将ZERO字节保存到我的流(VB.net)

时间:2014-12-28 13:17:36

标签: vb.net protobuf-net

我有从ProtoGen.exe生成的以下.vb代码

' Generated from: proto/Test.proto
Namespace axBasic
<Global.System.Serializable, Global.ProtoBuf.ProtoContract(Name:="Test")> _
Public Partial Class Test
    implements Global.ProtoBuf.IExtensible

    Public Sub New
    End Sub

    Private _id As UInteger
    <Global.ProtoBuf.ProtoMember(1, IsRequired:=True, Name:="id", DataFormat:=Global.ProtoBuf.DataFormat.TwosComplement)> _ 
    Public Property id As UInteger
        Get 
            Return _id
        End Get

        Set(value As UInteger)
        _id = value 

        End Set
    End Property

    Private extensionObject As Global.ProtoBuf.IExtension
        Function GetExtensionObject(createIfMissing As Boolean) As Global.ProtoBuf.IExtension Implements Global.ProtoBuf.IExtensible.GetExtensionObject
            Return Global.ProtoBuf.Extensible.GetExtensionObject(extensionObject, createIfMissing)
        End Function
End Class

End Namespace

Test.Proto文件非常简单:

package axBasic;

message Test {
  required uint32  id = 1;
}

我尝试使用以下代码生成一个简单的.bin文件,但它总是ZERO字节

Dim t = New axBasic.Test With {.id = 1}
ProtoBuf.Serializer.Serialize(IO.File.Create("1.bin"), t)

请错过一步?为什么这不起作用?

1 个答案:

答案 0 :(得分:1)

零字节在protobuf中实际上是完全合法的,虽然我只希望如果它是所有默认值(id = 0可能会这样做)。实际上,我怀疑这只是一个没有正确关闭文件的情况。如注释中所述,您应该在文件周围使用using语句。在c#术语中:

using(var file = IO.File.Create("1.bin")) {
    ProtoBuf.Serializer.Serialize(file, t);
}