C# 使用protobuf net为C+序列化对象数组+;消费

C# 使用protobuf net为C+序列化对象数组+;消费,c#,c++,protobuf-net,C#,C++,Protobuf Net,我已经按照以下答案中的说明构建了一些类,以便能够序列化泛型对象列表;例如KeyValuePair和KeyValuePair的实例 不幸的是,GETStudio()方法生成的.PROTO文件没有生成一个可以为C++正确解析的文件。为数组生成的子类型消息的后缀为“[]”。在编译C++时,该协议的“EXE”会被阻塞。p> 由于protobuf的消息名称似乎是任意的(即,它们实际上不包含在数据流中),因此在命名子类型时,是否可以告诉protobuf net使用“_Array”而不是“[]”?或者,我还有

我已经按照以下答案中的说明构建了一些类,以便能够序列化泛型对象列表;例如KeyValuePair和KeyValuePair的实例

不幸的是,GETStudio()方法生成的.PROTO文件没有生成一个可以为C++正确解析的文件。为数组生成的子类型消息的后缀为“[]”。在编译C++时,该协议的“EXE”会被阻塞。p> 由于protobuf的消息名称似乎是任意的(即,它们实际上不包含在数据流中),因此在命名子类型时,是否可以告诉protobuf net使用“_Array”而不是“[]”?或者,我还有其他的途径,可以生成一个C++应用程序? 谢谢

下面是相关代码和生成的原型文件

基类是:

[DataContract]
[ProtoInclude(101, typeof(KeyValuePairResponse<string>))]
[ProtoInclude(102, typeof(KeyValuePairResponse<int>))]
[ProtoInclude(103, typeof(KeyValuePairResponse<double>))]
[ProtoInclude(111, typeof(KeyValuePairResponse<string[]>))]
[ProtoInclude(112, typeof(KeyValuePairResponse<int[]>))]
[ProtoInclude(113, typeof(KeyValuePairResponse<double[]>))]
public abstract class KeyValuePairResponse
{
    protected KeyValuePairResponse() { }

    [DataMember(Order = 1, IsRequired = true)]
    public string Key { get; set; }

    public object Value
    {
        get
        {
            return this.ValueImplementation;
        }

        set
        {
            this.ValueImplementation = value;
        }
    }

    protected abstract object ValueImplementation { get; set; }

    public static KeyValuePairResponse<T> Create<T>(string key, T value)
    {
        return new KeyValuePairResponse<T>(key, value);
    }
}
[DataContract]
public sealed class KeyValuePairResponse<T> : KeyValuePairResponse
{
    public KeyValuePairResponse()
    {
    }

    public KeyValuePairResponse(string key, T value)
    {
        this.Key = key;
        this.Value = value;
    }

    [DataMember(Order = 2, IsRequired = true)]
    public new T Value { get; set; }

    protected override object ValueImplementation
    {
        get
        {
            return this.Value;
        }

        set
        {
            this.Value = (T)value;
        }
    }
}

这只是GetProto中的一个bug。我建议将其记录在github protobuf网络列表中,或者如果您有冒险精神,甚至可以提交一个请求


现在:Ctrl+h(查找并替换)可能是您的朋友。

谢谢Marc。一般来说,我对protobuf是新手,但我怀疑这些名字很容易被替换。当我有时间(lol)时,我将查看github存储库。
message KeyValuePairResponse {
   required string Key = 1;
   // the following represent sub-types; at most 1 should have a value
   optional KeyValuePairResponse_String KeyValuePairResponse_String = 101;
   optional KeyValuePairResponse_Int32 KeyValuePairResponse_Int32 = 102;
   optional KeyValuePairResponse_Double KeyValuePairResponse_Double = 103;
   optional KeyValuePairResponse_String[] KeyValuePairResponse_String[] = 111;
   optional KeyValuePairResponse_Int32[] KeyValuePairResponse_Int32[] = 112;
   optional KeyValuePairResponse_Double[] KeyValuePairResponse_Double[] = 113;
}
message KeyValuePairResponse_Double {
   required double Value = 2 [default = 0];
}
message KeyValuePairResponse_Double[] {
   repeated double Value = 2;
}
message KeyValuePairResponse_Int32 {
   required int32 Value = 2 [default = 0];
}
message KeyValuePairResponse_Int32[] {
   repeated int32 Value = 2;
}
message KeyValuePairResponse_String {
   required string Value = 2;
}
message KeyValuePairResponse_String[] {
   repeated string Value = 2;
}