将 ProtoBuf 文件解析为对象列表

Parse ProtoBuff file to object list

嗨,我有一个像这样的 protobuff TXT 文件:

parameter {
  name: "Key1"
  value: 0
}
parameter {
  name: "Key2"
  value: 1.5
}
parameter {
  name: "Key3"
  text: "Value 3"
}
parameter {
  name: "Key4"
  arr {
    val: 0
    val: 2000
    val: 2050
    val: 2100
    val: 2150
    val: 2200
    val: 2250
    val: 2300
    val: 2350
    val: 2400
    val: 2400
    val: 2400
    dim: 12
    dim: 1
  }
}

我试图用这个 library

来解析它

当前试用码:

[ProtoContract]
public class Parameter
{
    [ProtoMember(1)]
    public string Name { set; get; }
    [ProtoMember(2)]
    public double Value { set; get; }
}
public class FileParser
{        
    public static Parameter[]  Parse(string filePath)
    {
        List<Parameter> parameters;
        using (var file = File.OpenRead(filePath))
        {
            parameters = Serializer.Deserialize<List<Parameter>>(file);
        }

        return parameters?.ToArray();
    }
    private FileParser()
    {

    }
}

我是 Proto-Buff 的新手,所以我不知道我的方向是否正确

这些属性建议protobuf-net; protobuf-net 仅实现 binary 协议,而不是自以为是的 JSON 协议。因此,您需要使用 Google protobuf 实现,通过 运行 架构 (.proto) 通过 protoc。请注意,通过更改此处的下拉列表也可以使用 protoc:https://protogen.marcgravell.com - 以防万一您不必下载任何其他工具。