Why string interning on serialization in protobuf-net does not work in this example? [closed]
[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.
精彩评论