Protocol Buffers NET 以序列化 SortedDictionary<CustomTKey, CustomTValue>?



使用protobuf.net和sortedDictionary时是否有任何已知问题?具体来说,我们使用的是SortedDictionary<CustomTKey, CustomTValue>(具有适当的IComparer<CustomTKey>实现,但这可能与Protobuf.net的焦点无关)

它正在我们的最后进行一些基本测试,但想知道是否有任何已知问题或疑虑。

如果工作起作用,工作是否以一般方式扩展到BCL中的其他强型收集类(例如:字典,sortedlist等)?我知道它与List S!

非常有效

protobuf-net试图避免编码到任何特定收集类型,而是尝试检测某些键模式;特别是,任何看起来像IListICollection<T>,甚至只是自定义迭代器和Add(Foo)的任何东西,其中GetEnumerator()返回.Current的迭代器,其中CC_8是类型Foo

SortedDictionary<TKey,TValue>实现了ICollection<KeyValuePair<TKey, TValue>>,这是Protobuf-net检测和使用的内容;因此,图书馆将其简单地视为键值配对的集合,调用.Add等按照该顺序(构造函数为KeyValuePair(TKey key, TValue value),因此.Key映射为字段1,而.Value映射为字段2)。将其放在一起,Protobuf-net地图:

[ProtoContract]
class Foo
{
    private readonly SortedDictionary<string, int> items =
        new SortedDictionary<string, int>(
        StringComparer.InvariantCultureIgnoreCase);
    [ProtoMember(1)]
    SortedDictionary<string, int> Items { get {  return items; } }
}

通过架构(通过Serializer.GetProto<Foo>()进行验证):

message Foo {
   repeated KeyValuePair_String_Int32 Items = 1;
}
message KeyValuePair_String_Int32 {
   optional string Key = 1;
   optional int32 Value = 2;
}

基本上,它应该正常工作;唯一要观看的是确保应用任何自定义IComparer<CustomTKey>。我仅通过给Protobuf-net访问 getter 并在我们的自定义类型中执行藏品启动来完成此操作 - 但是,如果您也可以使用预选的回调来执行相同的操作。您想启用构造函数。

最新更新