【问题标题】:protobuf-net code generatorprotobuf-net 代码生成器
【发布时间】:2014-08-22 17:48:17
【问题描述】:

我正在将 Google Proto Buffers 合并到我的项目中,并且非常好 protoc.exe for windows 如何为序列化/反序列化生成头文件和源文件。我需要 protobuf.net 代码生成器。因此,例如,如果我是一个 Person 类,我首先需要在 .proto 文件中定义它。然后当我在它上面运行 protoc.exe 时,它​​会给我 .h 和 .c 文件。但随后手动,我必须为 Person 类执行以下操作:

[ProtoContract]
class Person {
    [ProtoMember(1)]
    public int Id {get;set;}
    [ProtoMember(2)]
    public string Name {get;set:}
    [ProtoMember(3)]
    public Address Address {get;set;}
}
[ProtoContract]
class Address {
    [ProtoMember(1)]
    public string Line1 {get;set;}
    [ProtoMember(2)]
    public string Line2 {get;set;}
}

我希望一切都自动化。因此,从技术上讲,我想为类 Person 提供 .proto 文件并获得上述输出作为回报。我不想做手工工作。我可以自己编写那个工具。但如果已经有一种被广泛使用的工具,我宁愿使用那个工具,而不是重新发明轮子。

【问题讨论】:

    标签: c# .net protocol-buffers protobuf-net


    【解决方案1】:

    这里有一个 C# protobuf 生成器:

    https://code.google.com/p/protobuf-csharp-port/wiki/ProtoGen

    它可以从 protoc.exe 获取 protobin 输出,也可以获取 .proto 文件并透明地为您调用 protoc,然后将输出转换为 C#。 它是开源的,所以如果你需要调整它生成类的方式,你可以。

    【讨论】:

      【解决方案2】:

      .NET 5 有一个 grpc 模板,因此您可以使用 dotnet new grpc 创建一个新项目。它包含一个 grpc 服务器。如果在生成的项目中修改 greet.proto 文件中的 HelloRequest 消息并再次编译,您会看到新生成了一个HelloRequest 类。 该项目引用了Grpc.AspNetCore nuget 包。

      Grpc.AspNetCore 是一个元包,其中引用了:

      • Grpc.AspNetCore.Server:.NET 的 gRPC 服务器库。
      • Grpc.Tools:代码生成工具包。
      • Google.Protobuf:Protobuf 序列化库。

      (见Grpc.AspNetCore)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-02
        • 2010-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多