Protocol buffer serializing with inheritance. Deri

2019-09-19 02:05发布

问题:

I faced with strange problem. After reading a lot, I tried to deploy protobuf-net in my app. Classes binds with simple inheritance. After presumably successful serialization, I am trying to deserialize to list, but protobuf filling only base class!

here a bit of code:

serializing function

 private static void serialize<T>(T obj) where T: Log
  { 
    using (var fileStream = 
    new FileStream(fileName, FileMode.Append))
    {
       Serializer.SerializeWithLengthPrefix(fileStream, obj, 
         PrefixStyle.Base128, SerializeTypesDictionary.First(x =>
                              x.Value == obj.GetType()).Key);

    }
  }

deserialize

 private static ArrayList deserialize(string filename)
    {           
        object obj;
        var arr = new ArrayList();
        using (var fileStream = new FileStream(file, FileMode.Open))
        {
            while (Serializer.NonGeneric.TryDeserializeWithLengthPrefix
                  (fileStream, PrefixStyle.Base128, resolver, out obj))
            {
                arr.Add(obj);
            }
        }
        return arr;
    }

and classes

 [ProtoContract(ImplicitFields = ImplicitFields.AllPublic)
 [ProtoInclude(100, typeof(Error))]
 [ProtoInclude(101, typeof(Record))]
 [ProtoInclude(102, typeof(SqlQuery))]
 public class Log
 {...}

 [ProtoContract(ImplicitFields = ImplicitFields.AllPublic)]
 public class Error:Log
 {...}

and other with same structure

So, what am I doing wrong?

P.S. after deserializing list consists right classes (not base! all classes deserialised in their types), but with empty fields, and full fields in base class.

回答1:

The question closed.

Sorry for wasting yours time, if you wasted it=) Simply forgot attributes on one of the derived classes, and somehow its affect to others.