Skip to content

Instantly share code, notes, and snippets.

@gyurisc
Created August 21, 2015 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gyurisc/23bd46e2149d81ac148e to your computer and use it in GitHub Desktop.
Save gyurisc/23bd46e2149d81ac148e to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProtoBuf;
namespace TestProtoBufSerialization
{
class Protobuf_CyclicIssue
{
void Main()
{
var st = new ST() { Id = "ST001" };
var si = new SI() { Id = "SI001" };
st.Indexes = new List<SI>();
st.Indexes.Add(si);
si.ST = st;
ST newST = serializeDeserializeWithProto<ST>(st, "testing_cyclic_references");
Debug.Assert(st != null, "ST is null!");
}
private static T serializeDeserializeWithProto<T>(T input, string fileName)
{
using (var file = File.Create(fileName + ".bin"))
{
Serializer.Serialize(file, input);
}
T output;
using (var file = File.OpenRead(fileName + ".bin"))
{
output = Serializer.Deserialize<T>(file);
}
string proto = Serializer.GetProto<T>();
File.WriteAllText(typeof(T).ToString() + "_proto.txt", proto, Encoding.ASCII);
Console.WriteLine(proto);
return output;
}
#region public repro
[ProtoContract(ImplicitFields = ImplicitFields.AllPublic, AsReferenceDefault = true)]
[ProtoInclude(101, typeof(ST))]
[ProtoInclude(102, typeof(SI))]
public class Base
{
public string CreateBy { get; set; }
public string ModifiedBy { get; set; }
}
[ProtoContract(ImplicitFields = ImplicitFields.AllPublic, AsReferenceDefault = true)]
public class ST : Base
{
public string Id { get; set; }
[ProtoMember(103, AsReference=true)]
public List<SI> Indexes { get; set; }
}
[ProtoContract(ImplicitFields = ImplicitFields.AllPublic, AsReferenceDefault = true)]
public class SI : Base
{
public string Id { get; set; }
public ST ST { get; set; }
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment