protobuf-net 是否支持命名元组?

Does protobuf-net support named tuples?

protobuf-net是否支持named tuples的序列化?

例如

 [ProtoMember(1)]
 protected readonly SortedDictionary<double, (double Bid, double Ask, double Open, double High, double Low, double Close, int Volume, int OpenInt)> FuturesCurveData;

"Partly yes, partly no".

protobuf-net 没有关于命名元组的特定知识the names are not available to library code,但是从 v2.2.0 开始,protobuf-net 可以 来推断 ValueTuple<...> 类型的合同,本质上将其视为位置类型 - 因此 Bid 将是字段 1,Ask 将是字段 2,等等。一些代码像 GetProto<T>not 产生你期望的结果(因为 a:它看不到名字,b:对于大元组,形状变得很奇怪),你将无法控制细粒度的序列化细节(DataFormat,等等),但是:它应该可以工作。

以下工作正常:

using ProtoBuf;
using System.Collections.Generic;

static class P
{
    static void Main()
    {
        var obj = new MyType { FuturesCurveData = {
                { 1.0, (1, 2, 3, 4, 5, 6, 7, 8) },
                { 2.0, (2, 3, 4, 5, 6, 7, 8, 9) },
            } };
        var clone = Serializer.DeepClone(obj);
        foreach(var pair in clone.FuturesCurveData)
        {
            System.Console.WriteLine($"{pair.Key}: {pair.Value}");
        }
    }
}
[ProtoContract]
class MyType
{
    [ProtoMember(1)]
    public SortedDictionary<double, (double Bid, double Ask, double Open, double High, double Low, double Close, int Volume, int OpenInt)> FuturesCurveData { get; } =
    new SortedDictionary<double, (double Bid, double Ask, double Open, double High, double Low, double Close, int Volume, int OpenInt)>();
}

输出:

1: (1, 2, 3, 4, 5, 6, 7, 8)
2: (2, 3, 4, 5, 6, 7, 8, 9)

下面是 GetProto<MyType>() 产生的结果 - 不太成功:

syntax = "proto2";
package System;

message KeyValuePair_Double_ValueTuple_Double_Double_Double_Double_Double_Double_Int32_ValueTuple_Int32 {
   optional double Key = 1;
   optional ValueTuple_Double_Double_Double_Double_Double_Double_Int32_ValueTuple_Int32 Value = 2;
}
message MyType {
   repeated KeyValuePair_Double_ValueTuple_Double_Double_Double_Double_Double_Double_Int32_ValueTuple_Int32 FuturesCurveData = 1;
}
message ValueTuple_Double_Double_Double_Double_Double_Double_Int32_ValueTuple_Int32 {
   optional double Item1 = 1;
   optional double Item2 = 2;
   optional double Item3 = 3;
   optional double Item4 = 4;
   optional double Item5 = 5;
   optional double Item6 = 6;
   optional int32 Item7 = 7;
   optional ValueTuple_Int32 Rest = 8;
}
message ValueTuple_Int32 {
   optional int32 Item1 = 1;
}

附带说明一下,如果密钥类型为 intstring,它 应该 用作 map<,>,但它出现遇到一个错误,I have logged.