[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。
感谢。