protobuf-net:无法反序列化ReadOnlyCollection

问题描述 投票:9回答:1

我正在尝试使用protobuf-net对ReadOnlyCollection进行序列化和反序列化。但是,当protobuf-net尝试将List转换为ReadOnlyCollection时,反序列化时会抛出异常。

        var roc = new ReadOnlyCollection<byte>(new byte[] {1, 2, 3});
        var ms = new MemoryStream();

        Serializer.Serialize(ms, roc);
        ms.Position = 0;
        var roc2 = Serializer.Deserialize<ReadOnlyCollection<byte>>(ms);

        Console.WriteLine( BitConverter.ToString( roc2.ToArray() ) );

有没有办法把它保持为ReadOnlyCollection而不是序列化/反序列化为List?在实际的应用程序中,ReadOnlyCollection是我想要序列化的不可变对象的一部分,并且宁愿将它保留为ReadOnlyCollection

c# protobuf-net readonly-collection
1个回答
0
投票

我认为protobuf-net只将集合反序列化为List。你可以:

var roc2aux = Serializer.Deserialize<List<byte>>(ms);
var roc2 = new ReadOnlyCollection<byte>(roc2aux);
Console.WriteLine( BitConverter.ToString( roc2.ToArray() ) );
© www.soinside.com 2019 - 2024. All rights reserved.