开发者

Why string interning on serialization in protobuf-net does not work in this example? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.
  [ProtoContract]
  public class A
  {
    [ProtoMember(1, AsReference = true)]
    public stri开发者_开发问答ng Id { get; set; }

    public override bool Equals(object obj) { return Id == ((A)obj).Id; }
    public override int GetHashCode() { return Id.GetHashCode(); }
    public override string ToString() { return Id; }
  }
  [ProtoContract]
  public class B
  {
    [ProtoMember(1)]
    public string Id { get; set; }

    public override bool Equals(object obj) { return Id == ((B)obj).Id; }
    public override int GetHashCode() { return Id.GetHashCode(); }
    public override string ToString() { return Id; }
  }

  class Program
  {
    static void Main()
    {
      var m = RuntimeTypeModel.Default;
      m.Add(typeof(object), false).AddSubType(1, typeof(A)).AddSubType(2, typeof(B));

      var list = new List<object> { new A { Id = "Abracadabra" }, new B { Id = "Focuspocus" }, new A { Id = "Abracadabra" }, };
      using (var ms = new MemoryStream())
      {
        m.Serialize(ms, list);
        ms.Position = 0;
        var list2 = (List<object>)m.Deserialize(ms, null, typeof(List<object>));
        Debug.Assert(list.SequenceEqual(list2));
        File.WriteAllBytes(@"output.dump", ms.ToArray());
      }
    }
  }

The produced output.dump file contains two instances of the Abracadabra string, while there should be only one (how do I enable string interning in protobuf-net?).

I am using v2 rev 421.

Thanks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜