安装包
| Google.Protobuf |
| Google.Protobuf.Tools |
Google.Protobuf.Tools中存在protoc.exe可以用来编译.proto文件
编写 Person.proto
文件
| syntax = "proto3"; |
| |
| message Person { |
| string name = 1; |
| int32 id = 2; |
| string email = 3; |
| } |
使用protoc.exe编译
得到 Person.cs
文件,将其引入到项目中
在c#中使用
| using Google.Protobuf; |
| |
| |
| Person person = new Person { Name = "Alice", Id = 123, Email = "alice@example.com" }; |
| byte[] data = person.ToByteArray(); |
| Console.WriteLine($"Length:{data.Length}"); |
| Console.WriteLine($"{Convert.ToBase64String(data)}"); |
| |
| |
| Person newPerson = Person.Parser.ParseFrom(data); |
| Console.WriteLine($"Name:{newPerson.Name}"); |
| Console.Read(); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步