c# - protobuf-net 代码生成器

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

我正在将 Google Proto Buffers 合并到我的项目中,Windows 的 protoc.exe 生成用于序列化/反序列化的头文件和源文件的方式非常好。我需要 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# protobuf 生成器:

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

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

关于c# - protobuf-net 代码生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25452839/

相关文章:

tcp - Protocol Buffer Wireshark 插件

C# .net Protocol Buffer - protobuf-net 支持序列化对象值字典吗?

c# - .NET HttpClient GET 请求在空闲约 100 秒后非常慢

c# - 给定相同的盐、字符串和因子,BCrypt 生成不同的哈希值

c# - 删除一对多关系中的相关实体

c# - 如何验证Azure负载均衡器?

c# - "The process cannot access the file X because it is used by another process"-C#

.net - 如何重置 .NET Windows Forms TextBox BackColor 属性?

java - 生成 java 类时无法使用 maven protobuf-maven-plugin 插件解析 google protobuf 文件中的导入语句

c# - 错误 0004 : Could not load file or assembly 'MySql. 数据.Entity,版本=6.9.7.0