【发布时间】:2016-05-06 20:12:22
【问题描述】:
在使用涉及泛型和继承的更复杂层次结构时,尝试使用 protobuf-net 执行反序列化时遇到了一些问题。
动态定义类型层次的方法是什么?
有趣的是序列化工作正常,但反序列化失败并出现异常消息:“无法为:Serialization.command.ICommand 准备序列化程序”
请看下面我正在使用的代码:
[ProtoContract]
public abstract class AbstractEntity
{
public AbstractEntity()
{
}
protected AbstractEntity(int integer)
{
this.integer = integer;
}
private int noSerializable = 0;
[ProtoMember(1)]
public int NoSerializable { get { return noSerializable; }
set { noSerializable=value; }
}
}
[ProtoContract]
public class Entity:AbstractEntity
{
public Entity() : base()
{
}
public Entity(int integer) : base(integer)
{
}
[ProtoMember(2)]
public String Name { get; set; }
[ProtoMember(3)]
public String Surname { get; set; }
[ProtoMember(4)]
public int Age { get; set; }
}
[ProtoContract]
public interface ICommand
{
void Execute();
}
[ProtoContract]
public abstract class AbstractCommand: ICommand
{
public abstract void someAbstractMet();
public void Execute()
{
}
}
[ProtoContract]
public class AbstractEntityCommand<T>:AbstractCommand where T:AbstractEntity
{
[ProtoMember(1)]
public T Entity { set; get; }
public AbstractEntityCommand()
{
}
public AbstractEntityCommand(T entity)
{
Entity = entity;
}
public override void someAbstractMet()
{
}
}
[ProtoContract]
public class UpdateEntityCommand<T>:AbstractEntityCommand<T> where T:AbstractEntity
{
public UpdateEntityCommand()
{
}
public UpdateEntityCommand(T entity):base(entity)
{
}
public override void someAbstractMet()
{
}
}
class Program
{
static void Main(string[] args)
{
var entity = new Entity(4);
Console.WriteLine("Using Protobuf");
//first define dinamicaly the hierarchy - see ProtoInclude attribute
RuntimeTypeModel.Default.Add(typeof (AbstractEntity), true).AddSubType(7, typeof (Entity));
AbstractEntity absEnt = entity;
bites = absEnt.ProtoBufSerialize();
thirdEntity = (Entity)bites.ProtoBufDeserialize<AbstractEntity>();
Console.WriteLine("Name=" + thirdEntity.Name + " Surname=" + thirdEntity.Surname + " Age=" + thirdEntity.Age + " integer=" + thirdEntity.TheInt + " No serializable field = " + thirdEntity.NoSerializable);
// Console.ReadKey();
Console.WriteLine("Test for generics");
RuntimeTypeModel.Default.Add(typeof(ICommand), true).AddSubType(70, typeof(AbstractCommand));
RuntimeTypeModel.Default.Add(typeof(ICommand), true).AddSubType(80, typeof(AbstractEntityCommand<>));
RuntimeTypeModel.Default.Add(typeof(ICommand), true).AddSubType(90, typeof(UpdateEntityCommand<>));
ICommand genCommand = new UpdateEntityCommand<Entity>(entity);
bites = genCommand.ProtoBufSerialize();
var concreteCmd = bites.ProtoBufDeserialize<UpdateEntityCommand<Entity>>();
var unknownCmd = bites.ProtoBufDeserialize<ICommand>();
Console.ReadKey();
}
在我尝试反序列化 ICommand 之前,一切正常。
我在这里错过了什么?
【问题讨论】:
标签: generics inheritance serialization protobuf-net