带有继承的协议缓冲区序列化.派生类为空 [英] Protocol buffer serializing with inheritance. Derived classes are empty
问题描述
我遇到了一个奇怪的问题.看了很多书后,我尝试在我的应用程序中部署protobuf-net.类通过简单的继承进行绑定.在大概成功的序列化之后,我尝试对列表进行反序列化,但是protobuf仅填充基类!
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!
这里是一些代码:
序列化功能
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);
}
}
反序列化
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;
}
和课程
[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
{...}
和其他具有相同结构的
那么,我在做什么错了?
So, what am I doing wrong?
P.S.反序列化后的列表包括正确的类(不是基类!所有按其类型反序列化的类),但基类中包含空字段和完整字段.
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.
推荐答案
问题已解决.
很抱歉浪费您的时间,如果您浪费了==)只是忘记了其中一个派生类的属性,并以某种方式对其他类产生了影响.
Sorry for wasting yours time, if you wasted it=) Simply forgot attributes on one of the derived classes, and somehow its affect to others.
这篇关于带有继承的协议缓冲区序列化.派生类为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!