- â . ã¯ããã«
- â ¡. ããæ¹ï¼Google.Protobufã使ãæ¹æ³ï¼
- â ¢. ããæ¹ï¼protobuf-netã使ãæ¹æ³ï¼
- â £. Google.Protobuf (protoc.exe)㨠protobuf-net (protogen.exe) ã®éã
â . ã¯ããã«
ã¿ã¤ãã«ã®éããC#ã§ProtocolBuffersã使ãæ¹æ³ãã§ãã
æ¬è¨äºã§ã¯proto3ï¼ProtocolBuffers version 3ï¼ã対象ã¨ãã¾ãã
ã¾ããprotoãã¡ã¤ã«ãæ¸ãæ㯠VisualStudio Codeã« vscode-proto3 ãã¤ã³ã¹ãã¼ã«ãã¦éçºããäºããããããã¾ãã
ã¾ããProtocolBuffersããªãã¼ã¹ããæ¹æ³ã¯ãã¡ã
https://kagasu.hatenablog.com/entry/2017/03/17/124259
â ¡. ããæ¹ï¼Google.Protobufã使ãæ¹æ³ï¼
1. NuGetãã Google.Protobuf ãã¤ã³ã¹ãã¼ã«ãã
Install-Package Google.Protobuf
2. protoãã¡ã¤ã«ãä½æãã
Human.proto
syntax = "proto3"; package MyPackage; message Human { string name = 1; uint32 age = 2; }
3. protoc.exe ããã¦ã³ãã¼ããã
4. protoãã¡ã¤ã«ããC#ã®ã¯ã©ã¹ãèªåçæãã
protoc.exe --csharp_out=../ProtoModel Human.proto
5. ãµã³ãã«ããã°ã©ã ãæ¸ã
using Google.Protobuf; using MyPackage; static void Main(string[] args) { var human = new Human { Name = "name001", Age = 20 }; byte[] bytes = human.ToByteArray(); Console.WriteLine(BitConverter.ToString(bytes)); // ãã·ãªã¢ã©ã¤ãº // human = Human.Parser.ParseFrom(bytes); }
å®è¡çµæ
â ¢. ããæ¹ï¼protobuf-netã使ãæ¹æ³ï¼
1. NuGetãã protobuf-net ãã¤ã³ã¹ãã¼ã«ãã
Install-Package protobuf-net
2. protoãã¡ã¤ã«ãä½æãã
Human.proto
syntax = "proto3"; package MyPackage; message Human { string name = 1; uint32 age = 2; }
3. protogen.exe ããã¦ã³ãã¼ããã
4. protoãã¡ã¤ã«ããC#ã®ã¯ã©ã¹ãèªåçæãã
protogen.exe --csharp_out=../ProtoModel Human.proto
5. ãµã³ãã«ããã°ã©ã ãæ¸ã
using ProtoBuf; using MyPackage; static void Main(string[] args) { var human = new Human { Name = "name001", Age = 20 }; using (var ms = new MemoryStream()) { Serializer.Serialize(ms, human); byte[] bytes = ms.ToArray(); Console.WriteLine(BitConverter.ToString(bytes)); // ãã·ãªã¢ã©ã¤ãº // human = Serializer.Deserialize<Human>(ms); } }
å®è¡çµæ