是否可以使用protobuf-net序列化以下类?我看了一遍,我找不到这种情况的任何例子?
[ProtoContract]
public class Example
{
[ProtoMember(1)]
public ConcurrentDictionary<int, string> CategoryNames;
[ProtoMember(2)]
public ConcurrentDictionary<int, int[]> TaxonomyMap;
[ProtoMember(3)]
public ConcurrentDictionary<int, Dictionary<int, int>>[] Tokens;
}
我目前获得标准&#34;嵌套或锯齿状列表和数组不受支持&#34;。但是,我不确定如何映射这样的课程?
修改 根据@ Marc的建议,我能够成功地使用protobuf-net来序列化上面的类。为了完整起见,我在下面添加了我的解决方案:
创建了以下类,其中包含ModelPartition对象列表:
[ProtoContract]
public class GenomeModel
{
[ProtoMember(1)]
public ConcurrentDictionary<int, string> categoryNames =
new ConcurrentDictionary<int, string>();
[ProtoMember(2)]
public ConcurrentDictionary<int, int[]> taxonomyMap =
new ConcurrentDictionary<int, int[]>();
[ProtoMember(3)]
public List<ModelPartition> geneTokens = new List<ModelPartition>();
}
ModelPartition对象用于&#34;抽象&#34; ConcurrentDictionary数组通过将每个索引位置和单个ConcurrentDictionary成员保存在单独的ModelPartition对象中,然后将其放在GenomeModel.geneTokens ModelPartition列表中:
[ProtoContract]
public class ModelPartition
{
[ProtoMember(1)]
public int partitionNo;
[ProtoMember(2)]
public ConcurrentDictionary<int, Dictionary<int, int>> partitionData;
public ModelPartition(int PartitionNo
, ConcurrentDictionary<int, Dictionary<int, int>> PartitionData)
{
partitionNo = PartitionNo;
partitionData = PartitionData;
}
}
最后,在序列化过程中,使用短转换从原始数据加载数据&#34;示例&#34;使用ModelPartition作为包装器对象将对象转换为GenomeModel对象:
GenomeModel genomeModel = new GenomeModel();
genomeModel.categoryNames = strand.categoryNames;
genomeModel.taxonomyMap = strand.taxonomyMap;
for (int i = 0; i < strand.minhashSignatureSize; i++)
{
genomeModel.geneTokens.Add(new ModelPartition(i, strand.geneTokens[i]));
}
using (var file = File.Create(commandAgrs[0]))
{
Serializer.Serialize<GenomeModel>(file, genomeModel);
}
谢谢马克!
答案 0 :(得分:1)
ConcurrentDictionary<TKey,TValue>
运行正常。问题是ConcurrentDictionary<TKey,TValue>[]
- 本质上是(从库的角度来看)与KeyValuePair<TKey, TValue>[][]
相同的布局 - 即嵌套数组。如果您需要,请考虑使用每个拥有字典的数组(或列表),而不是字典数组。