请观察以下简单程序(基于protobuf-net项目v1 wiki的示例): using System.Collections.Generic;using System.Diagnostics;using System.IO;using ProtoBuf;namespace HelloProtoBuf{ [ProtoContract] class Person { [ProtoMember(1)] publi
using System.Collections.Generic; using System.Diagnostics; using System.IO; using ProtoBuf; namespace HelloProtoBuf { [ProtoContract] class Person { [ProtoMember(1)] public int Id { get; set; } [ProtoMember(2)] public string Name { get; set; } [ProtoMember(3)] public Address Address { get; set; } } [ProtoContract] class Address { [ProtoMember(1)] public string Line1 { get; set; } [ProtoMember(2)] public string Line2 { get; set; } } class Program { static void Main(string[] args) { var person = new Person { Id = 12345, Name = "Fred", Address = new Address { Line1 = "Flat 1", Line2 = "The Meadows" } }; var person2 = new Person { Id = 4553, Name = "Nadya", Address = person.Address }; var persons = new List<Person> { person, person2 }; Debug.Assert(ReferenceEquals(persons[0].Address, persons[1].Address)); using (var file = File.Create("persons.bin")) { Serializer.Serialize(file, persons); } List<Person> persons2; using (var file = File.OpenRead("persons.bin")) { persons2 = Serializer.Deserialize<List<Person>>(file); } Debug.Assert(ReferenceEquals(persons2[0].Address, persons2[1].Address)); } } }
第二个断言失败了.这是protobuf-net实现中的错误还是协议缓冲区根本不支持具有共享引用的对象图?
谢谢.
协议缓冲区本身不支持这个 – 所以不,它不是一个bug.实际上,XmlSerializer和DataContractSerializer *会做同样的事情(JavaScriptSerializer和JSON.NET也可能这样做).但是,这是一个常见的请求,所以这在protobuf-net v2中得到支持(基本上:我作弊).只需将其更改为:
[ProtoMember(3, AsReference=true)] public Address Address { get; set; }
(并使用我在大约5分钟内上传的v2 dll,或者从代码构建)
* =警告:DataContractSerializer支持引用,但仅限于使用特定构造函数;它默认是禁用的