【问题标题】:Serializing ConcurrentQueue using protobuf使用 protobuf 序列化 ConcurrentQueue
【发布时间】:2015-09-09 09:09:40
【问题描述】:

我正在尝试使用 protobuf 序列化 ConcurrentQueue,但在反序列化对象时出现异常

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

有办法解决吗?喜欢为 Protobuf 编写扩展或继承和扩展 ConcurrentQueue?

【问题讨论】:

  • 为什么需要序列化并发队列?将项目出列并使用List<T> 或数组。您可以在反序列化后重新填充实际队列。

标签: c# .net serialization protocol-buffers protobuf-net


【解决方案1】:

protobuf 的开发人员已声明 here 不支持 ConcurrentQueue<T>,并提供了类似于 Lloyd 建议的解决方法。如果链接不再可用,请添加以下代码:

public ConcurrentQueue<int> Items {get;set;}

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

【讨论】:

    猜你喜欢
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多