知识
Protobuf
是一种数据格式的协议。为了将协议转换成不同编程语言对应的代码文件,需要使用不同的工具对 .proto
文件进行转换。
转换工具
- Protocol Buffers
- Protobuf-net (for c#)
- protobuf-csharp-port (for c# 、java)
编译 Protobuf-net
- 下载源代码 Protobuf-net
- 用
Rider
打开之后,运行dotnet build
。
- 在
protobuf-net-main\src\protobuf-net.Protogen\bin\Debug\net5.0\publish
里面会生成本机环境对应的ProtoGen
工具。 - 查看
ProtoGen
的使用:protogen.exe help
辅助工具
-
工具下载:
Java code for ProtoGenCopy, 提取码:
1q2e
ProtoGenCopy Exe for Window,提取码:
wtna
-
应用:比如
.proto
目录结构:+ proto - hello.proto + morning - say_good_morning.proto + afternoon - say_good_afternoon.proto + night - say_good_night.proto
转换成对应的结构:
+ proto - hello.cs + morning - say_good_morning.cs + afternoon - say_good_afternoon.cs + night - say_good_night.cs
-
工具配置文件
config.ini
,放在运行的程序的同级目录:# 字符串不需要用 "" 包裹 [ProtoGen] # progen.exe 的执行路径 protogen_exe_path=I:\Github\ProtoGenToCs-master\ProtoGen\protogen.exe # 放 .proto 文件的根目录 proto_source_path=I:\Github\ProtoGenToCs-master\Proto2Cs\Proto # 放生成的 .cs 文件的根目录 proto_csharp_target_path=I:\Github\ProtoGenToCs-master\Proto2Cs\Cs # 生成的 .cs 文件的命名空间,可以不写 proto_csharp_namespace=ProtoTest
其他工具