为什么在protobuf-net中序列化的字符串实习在这个例子中不起作用?

时间:2011-06-25 09:25:52

标签: .net protobuf-net

  [ProtoContract]
  public class A
  {
    [ProtoMember(1, AsReference = true)]
    public string 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());
      }
    }
  }

生成的output.dump文件包含两个Abracadabra字符串实例,而应该只有一个(how do I enable string interning in protobuf-net?)。

我正在使用v2 rev 421。

感谢。

0 个答案:

没有答案