簡體   English   中英

Protobuf-net序列化問題

[英]Protobuf-net serialization issue

這似乎是一個非常簡單的用例,我不理解如何拋出以下代碼片段中的異常。

static void Main(string[] args)
{
    using (var foobar = new MemoryStream())
    {
        ProtoBuf.Serializer.Serialize(foobar, new Foobar());
        if (foobar.Length == 0)
            throw new Exception("Didn't serialize");
    }
}

[ProtoContract]
public class Foobar
{
    [ProtoMember(1)]
    public int FoobarInt { get; set; }
}

ProtoBuf有點奇怪...長度為零的序列化形式本身不是“錯誤” ...

這樣想:

如果要對流進行反序列化並獲取嘗試序列化的空對象,則空流就足夠了(即,對象的“新”功能也可以做到),但是如果該對象上有任何數據集,現在您需要實際保存/加載內容

static void Main(string[] args)
{
    using (var foobar = new MemoryStream())
    {
        var foo = new Foobar() { FoobarInt = 1 };
        ProtoBuf.Serializer.Serialize(foobar, foo);
        if (foobar.Length == 0)
            throw new Exception("Didn't serialize");
    }
}

[ProtoContract]
public class Foobar
{
    [ProtoMember(1)]
    public int FoobarInt { get; set; }
}

現在產生一個字節數組0x08, 0x01

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM