protobuf-net : IExtensible not supported for inheritance

一世执手 提交于 2019-12-12 03:43:47

问题


It does not seem possible to implement protobuf-net serialization for classes that define their sub-types via [ProtoInclude] and implement ProtoBuf.IExtensible :

[ProtoBuf.ProtoInclude(1000, typeof(DerivedClass))]
public partial class BaseClass : ProtoBuf.IExtensible 
{ 
    ... 

    private IExtension extensionObject;
    IExtension IExtensible.GetExtensionObject(bool createIfMissing)
    {
        return Extensible.GetExtensionObject(ref extensionObject, createIfMissing);        
    }
}    

public partial class DerivedClass : BaseClass, ProtoBuf.IExtensible
{
    ... 

    private IExtension extensionObject;
    IExtension IExtensible.GetExtensionObject(bool createIfMissing)
    {
        return Extensible.GetExtensionObject(ref extensionObject, createIfMissing);        
    }
}   


var baseObject = new MyClass { ... };    
DerivedClass derivedObject;
using (var stream = new MemoryStream())
{
    Serializer.Serialize(stream, baseObject);  // throws runtime exception
    stream.Seek(0, SeekOrigin.Begin);
    derivedObject = Serializer.Deserialize<DerivedClass>(stream);
}

Attempt to serialize throws runtime exception :

{"IExtensible is not supported in structs or classes with inheritance"} at ProtoBuf.Serializers.TypeSerializer..ctor(TypeModel model, Type forType, Int32[] fieldNumbers, IProtoSerializer[] serializers, MethodInfo[] baseCtorCallbacks, Boolean isRootType, Boolean useConstructor, CallbackSet callbacks, Type constructType, MethodInfo factory)
at ProtoBuf.Meta.MetaType.BuildSerializer()
at ProtoBuf.Meta.MetaType.get_Serializer() at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) at ProtoBuf.Meta.TypeModel.SerializeCore(ProtoWriter writer, Object value) at ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value, SerializationContext context) at ProtoBuf.Serializer.Serialize[T](Stream destination, T instance) at ...

Specifically what is the purpose of implementing IExtensible ? and what are the consequences of not doing so ?

来源:https://stackoverflow.com/questions/40518369/protobuf-net-iextensible-not-supported-for-inheritance

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