protobuf-net v2 r450继承

时间:2011-10-22 09:53:28

标签: c# protobuf-net

protobuf-net v2 r450继承

希望有人可以通过protobuf-net帮助我指出正确的方向。我能找到的所有示例代码似乎都表明我正在尝试的应该是可能的。

域名(我的最终域名将比这更复杂,但我甚至无法实现这一点):

[ProtoContract]
public class GeographyData
{
    [ProtoMember(1)]
    public DateTime LastRefreshTime { get; set; }

    [ProtoMember(2, AsReference = true, IsRequired = true)]
    public GeographyItem Item { get; set; }
}

[ProtoContract]
[ProtoInclude(3, typeof(HotelData))]
public class GeographyItem
{
    [ProtoMember(1)]
    public Guid Id { get; set; }

    [ProtoMember(2)]
    public string Name { get; set; }
}

[ProtoContract]
public class HotelData : GeographyItem
{
}

测试

var geographyDataTest = new GeographyData();
geographyDataTest.LastRefreshTime = DateTime.Now;
geographyDataTest.Item = new HotelData
{
    Id = Guid.Empty,
    Name = "Test",
};

var test = ProtoBuf.Serializer.DeepClone(geographyDataTest);

例外:

无法将“Domain.Geography.GeographyItem”类型的对象强制转换为“Domain.Geography.HotelData”。

[InvalidCastException: Unable to cast object of type 'Domain.Geography.GeographyItem' to type 'Domain.Geography.HotelData'.]
   proto_14(Object , ProtoReader ) +2638
   ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Read(Object value, ProtoReader source) in C:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs:49
   ProtoBuf.Meta.RuntimeTypeModel.Deserialize(Int32 key, Object value, ProtoReader source) in C:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:413
   ProtoBuf.ProtoReader.ReadTypedObject(Object value, Int32 key, ProtoReader reader, Type type) in C:\Dev\protobuf-net\protobuf-net\ProtoReader.cs:534
   ProtoBuf.BclHelpers.ReadNetObject(Object value, ProtoReader source, Int32 key, Type type, NetObjectOptions options) in C:\Dev\protobuf-net\protobuf-net\BclHelpers.cs:428
   proto_2(Object , ProtoReader ) +688
   ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Read(Object value, ProtoReader source) in C:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs:49
   ProtoBuf.Meta.RuntimeTypeModel.Deserialize(Int32 key, Object value, ProtoReader source) in C:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:413
   ProtoBuf.Meta.TypeModel.DeepClone(Object value) in C:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:1034
   ProtoBuf.Serializer.DeepClone(T instance) in C:\Dev\protobuf-net\protobuf-net\Serializer.cs:46

有人能给我一些指示吗?

由于

1 个答案:

答案 0 :(得分:2)

有一个带有AsReference和继承的existing known glitch,我希望尽快解决。