0

以下代码尝试使用其接口序列化项目并反序列化:

class Program
{
    static void Main()
    {
        Item item = new Item { A = 123321 };

        using (MemoryStream ms = new MemoryStream())
        {
            Serializer.Serialize(ms, item);
            ms.Position = 0;
            Serializer.Deserialize<IItem>(ms);
        }
    }
}


[ProtoContract(SkipConstructor = true)]
[ProtoInclude(100, typeof(Item))]
public interface IItem
{
    int A { get; set; }
}

[ProtoContract(SkipConstructor = true)]
public class Item : IItem
{
    [ProtoMember(1)]
    public int A { get; set; }
}

尝试反序列化项目时引发以下异常:

The type cannot be changed once a serializer has been generated for Test.Item (Test.IItem)
   at ProtoBuf.Meta.RuntimeTypeModel.GetKey(Type type, Boolean demand, Boolean getBaseKey) in C:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 388
   at ProtoBuf.Meta.RuntimeTypeModel.GetKeyImpl(Type type) in C:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 362
   at ProtoBuf.Meta.TypeModel.GetKey(Type& type) in C:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 982
   at ProtoBuf.Meta.TypeModel.DeserializeCore(ProtoReader reader, Type type, Object value, Boolean noAutoCreate) in C:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 576
   at ProtoBuf.Meta.TypeModel.Deserialize(Stream source, Object value, Type type, SerializationContext context) in C:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 506
   at ProtoBuf.Meta.TypeModel.Deserialize(Stream source, Object value, Type type) in C:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 488
   at ProtoBuf.Serializer.Deserialize[T](Stream source) in C:\Dev\protobuf-net\protobuf-net\Serializer.cs:line 69
   at Test.Program.Main() in ...

有什么想法/解决方法吗?谢谢!

4

0 回答 0