使用protobuf序列化ConcurrentQueue

本文关键字:ConcurrentQueue 序列化 protobuf 使用 | 更新日期: 2023-09-27 17:59:05

我试图使用protobuf序列化ConcurrentQueue,但在反序列化对象时遇到异常

Type is not expected, and no contract can be inferred: System.Collections.Concurrent.ConcurrentQueue`1[[System.Byte[], mscorlib

有办法解决吗?比如给Protobuf写扩展,或者继承和扩展ConcurrentQueue?

使用protobuf序列化ConcurrentQueue

protobuf的开发人员在这里表示不支持ConcurrentQueue<T>,并给出了类似Lloyd建议的解决方法。添加以下代码以防链接不再可用:

public ConcurrentQueue<int> Items {get;set;}
[ProtoMember(n)]
private int[] Items
{
    get { return Items.ToArray(); }
    set { Items = new ConcurrentQueue<int>(value); }
}