是否可以将具有属性的聚合类型反序列化为作为引用的抽象基类型,请参阅 Aggregate.Base?如果没有,最好的解决方法是什么?
[ProtoContract]
[ProtoInclude(1, typeof(Derived))]
public abstract class Base { }
[ProtoContract]
public class Derived : Base
{
[ProtoMember(1)]
public int SomeProperty { get; set; }
}
[ProtoContract]
public class Aggregate
{
[ProtoMember(1, AsReference = true)]
public Base Base { get; set; }
}
[TestClass]
public class UnitTest
{
[TestMethod]
public void TestMethod1()
{
var value = new Aggregate { Base = new Derived() };
using (var stream = new MemoryStream())
{
Serializer.Serialize(stream, value);
stream.Position = 0;
// Raises an exception
// Unable to create type Sage.Estimating.Data.Base: Cannot create an abstract class.
Serializer.Deserialize<Aggregate>(stream);
}
}
}
在引发异常时调用堆栈:
protobuf-net.dll!ProtoBuf.BclHelpers.ReadNetObject(object value, ProtoBuf.ProtoReader source, int key, System.Type type, ProtoBuf.BclHelpers.NetObjectOptions options) Line 428 + 0xda bytes C# protobuf-net.dll!ProtoBuf.Serializers .NetObjectSerializer.Read(object value, ProtoBuf.ProtoReader source) Line 45 + 0x9f bytes C# protobuf-net.dll!ProtoBuf.Serializers.TagDecorator.Read(object value, ProtoBuf.ProtoReader source) Line 66 + 0x18 bytes C# protobuf-net .dll!ProtoBuf.Serializers.PropertyDecorator.Read(object value, ProtoBuf.ProtoReader source) Line 74 + 0x18 bytes C# protobuf-net.dll!ProtoBuf.Serializers.TypeSerializer.Read(object value, ProtoBuf.ProtoReader source) Line 205 + 0xf 字节 C# protobuf-net.dll!ProtoBuf.Meta.RuntimeTypeModel.Deserialize(int key, object value, ProtoBuf.ProtoReader source) Line 562 + 0xf bytes C# protobuf-net.dll!ProtoBuf.Meta.TypeModel.DeserializeCore(ProtoBuf.ProtoReader reader, System.Type type, object value, bool noAutoCreate) Line 634 + 0x14 bytes C# protobuf-net.dll !ProtoBuf.Meta.TypeModel.Deserialize(System.IO.Stream source, object value, System.Type type, ProtoBuf.SerializationContext context) Line 555 + 0x14 bytes C# protobuf-net.dll!ProtoBuf.Meta.TypeModel.Deserialize(System .IO.Stream source, object value, System.Type type) Line 534 + 0x13 bytes C# protobuf-net.dll!ProtoBuf.Serializer.Deserialize(System.IO.Stream source) Line 78 + 0x5a bytes C#Meta.TypeModel.Deserialize(System.IO.Stream source, object value, System.Type type, ProtoBuf.SerializationContext context) Line 555 + 0x14 bytes C# protobuf-net.dll!ProtoBuf.Meta.TypeModel.Deserialize(System.IO. Stream source, object value, System.Type type) Line 534 + 0x13 bytes C# protobuf-net.dll!ProtoBuf.Serializer.Deserialize(System.IO.Stream source) Line 78 + 0x5a bytes C#Meta.TypeModel.Deserialize(System.IO.Stream source, object value, System.Type type, ProtoBuf.SerializationContext context) Line 555 + 0x14 bytes C# protobuf-net.dll!ProtoBuf.Meta.TypeModel.Deserialize(System.IO. Stream source, object value, System.Type type) Line 534 + 0x13 bytes C# protobuf-net.dll!ProtoBuf.Serializer.Deserialize(System.IO.Stream source) Line 78 + 0x5a bytes C#