帮助需要用最简单的protobuf网为例 [英] Help needed with the most trivial protobuf-net example

查看:169
本文介绍了帮助需要用最简单的protobuf网为例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵守以下小事一件code:

  [ProtoContract]
  大众B级
  {
    [ProtoMember(1)公众诠释Ÿ;
  }

  [ProtoContract]
  公共C类
  {
    [ProtoMember(1)公众诠释Ÿ;
  }

  类节目
  {
    静态无效的主要()
    {
      变种B =新的B {Y = 2};
      变种C =新的C {Y = 4};
      使用(VAR毫秒=新的MemoryStream())
      {
        Serializer.Serialize(MS,B);
        Serializer.Serialize(MS,C);
        ms.Position = 0;
        VAR B2 = Serializer.Deserialize< B>(毫秒);
        Debug.Assert的(b.Y == b2.Y);
        变种C2 = Serializer.Deserialize&其中c取代;(毫秒);
        Debug.Assert的(c.Y == c2.Y);
      }
    }
  }
 

第一个断言失败! 每个序列化语句流位置前移通过2,所以在端ms.Position是4。然而,第一反序列化语句之后,该位置被设置为4,而不是2!事实上,b2.Y等于4,这应该是c2.Y的值<!/ P>

也有一些是绝对的基础,我很想念这里。我使用protobuf网V2 r383。

感谢。

修改

这一定有什么非常愚蠢的,因为它没有在V1擦出火花。

解决方案

您必须使用SerializeWithLength preFIX / DeserializeWithLength preFIX方法,以检索您的一个单一的对象。这应该工作:

 变种B =新的B {Y = 2};
变种C =新的C {Y = 4};
使用(VAR毫秒=新的MemoryStream())
{
    Serializer.SerializeWithLength preFIX(MS,B,prefixStyle.Fixed32);
    Serializer.SerializeWithLength preFIX(MS,C prefixStyle.Fixed32);
    ms.Position = 0;
    VAR B2 = Serializer.DeserializeWithLength preFIX&LT; B&GT;(MS,prefixStyle.Fixed32);
    Debug.Assert的(b.Y == b2.Y);
    VAR C2 = Serializer.DeserializeWithLength preFIX&LT; C&GT;(MS,prefixStyle.Fixed32);
    Debug.Assert的(c.Y == c2.Y);
}
 

Observe the following trivial piece of code:

  [ProtoContract]
  public class B
  {
    [ProtoMember(1)] public int Y;
  }

  [ProtoContract]
  public class C
  {
    [ProtoMember(1)] public int Y;
  }

  class Program
  {
    static void Main()
    {
      var b = new B { Y = 2 };
      var c = new C { Y = 4 };
      using (var ms = new MemoryStream())
      {
        Serializer.Serialize(ms, b);
        Serializer.Serialize(ms, c);
        ms.Position = 0;
        var b2 = Serializer.Deserialize<B>(ms);
        Debug.Assert(b.Y == b2.Y);
        var c2 = Serializer.Deserialize<C>(ms);
        Debug.Assert(c.Y == c2.Y);
      }
    }
  }

The first assertion fails! Each Serialize statement advances the stream position by 2, so in the end ms.Position is 4. However, after the first Deserialize statement, the position is set to 4, rather than 2! In fact, b2.Y equals 4, which should be the value of c2.Y!

There is something absolutely basic, that I am missing here. I am using protobuf-net v2 r383.

Thanks.

EDIT

It must be something really stupid, because it does not work in v1 either.

解决方案

You have to use the SerializeWithLengthPrefix/ DeserializeWithLengthPrefix methods in order to retrieve a single object from you stream. this should be working:

var b = new B { Y = 2 };
var c = new C { Y = 4 };
using (var ms = new MemoryStream())
{
    Serializer.SerializeWithLengthPrefix(ms, b,PrefixStyle.Fixed32);
    Serializer.SerializeWithLengthPrefix(ms, c, PrefixStyle.Fixed32);
    ms.Position = 0;
    var b2 = Serializer.DeserializeWithLengthPrefix<B>(ms,PrefixStyle.Fixed32);
    Debug.Assert(b.Y == b2.Y);
    var c2 = Serializer.DeserializeWithLengthPrefix<C>(ms, PrefixStyle.Fixed32);
    Debug.Assert(c.Y == c2.Y);
}

这篇关于帮助需要用最简单的protobuf网为例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆