Protocol buffer serializing with inheritance. Derived classes are empty

≯℡__Kan透↙ 提交于 2019-12-12 05:42:13

问题


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.



来源:https://stackoverflow.com/questions/29278037/protocol-buffer-serializing-with-inheritance-derived-classes-are-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!