主要分2种
P1:Google.Protobuf:分v2和v3;
类型对照表v3,地址见这里
Scalar Value Types
A scalar message field can have one of the following types – the table shows the type specified in the .proto file, and the corresponding type in the automatically generated class:
| .proto Type | Notes | C++ Type | Java Type | Python Type[2] | Go Type | Ruby Type | C# Type | PHP Type | Dart Type |
|---|---|---|---|---|---|---|---|---|---|
| double | double | double | float | float64 | Float | double | float | double | |
| float | float | float | float | float32 | Float | float | float | double | |
| int32 | Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint32 instead. | int32 | int | int | int32 | Fixnum or Bignum (as required) | int | integer | int |
| int64 | Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead. | int64 | long | int/long[3] | int64 | Bignum | long | integer/string[5] | Int64 |
| uint32 | Uses variable-length encoding. | uint32 | int[1] | int/long[3] | uint32 | Fixnum or Bignum (as required) | uint | integer | int |
| uint64 | Uses variable-length encoding. | uint64 | long[1] | int/long[3] | uint64 | Bignum | ulong | integer/string[5] | Int64 |
| sint32 | Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s. | int32 | int | int | int32 | Fixnum or Bignum (as required) | int | integer | int |
| sint64 | Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s. | int64 | long | int/long[3] | int64 | Bignum | long | integer/string[5] | Int64 |
| fixed32 | Always four bytes. More efficient than uint32 if values are often greater than 228. | uint32 | int[1] | int/long[3] | uint32 | Fixnum or Bignum (as required) | uint | integer | int |
| fixed64 | Always eight bytes. More efficient than uint64 if values are often greater than 256. | uint64 | long[1] | int/long[3] | uint64 | Bignum | ulong | integer/string[5] | Int64 |
| sfixed32 | Always four bytes. | int32 | int | int | int32 | Fixnum or Bignum (as required) | int | integer | int |
| sfixed64 | Always eight bytes. | int64 | long | int/long[3] | int64 | Bignum | long | integer/string[5] | Int64 |
| bool | bool | boolean | bool | bool | TrueClass/FalseClass | bool | boolean | bool | |
| string | A string must always contain UTF-8 encoded or 7-bit ASCII text, and cannot be longer than 232. | string | String | str/unicode[4] | string | String (UTF-8) | string | string | String |
| bytes | May contain any arbitrary sequence of bytes no longer than 232. | string | ByteString | str | []byte | String (ASCII-8BIT) | ByteString | string | List<int> |
V2的类型对照表,见这里
Scalar Value Types
A scalar message field can have one of the following types – the table shows the type specified in the .proto file, and the corresponding type in the automatically generated class:
| .proto Type | Notes | C++ Type | Java Type | Python Type[2] | Go Type |
|---|---|---|---|---|---|
| double | double | double | float | *float64 | |
| float | float | float | float | *float32 | |
| int32 | Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint32 instead. | int32 | int | int | *int32 |
| int64 | Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead. | int64 | long | int/long[3] | *int64 |
| uint32 | Uses variable-length encoding. | uint32 | int[1] | int/long[3] | *uint32 |
| uint64 | Uses variable-length encoding. | uint64 | long[1] | int/long[3] | *uint64 |
| sint32 | Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s. | int32 | int | int | *int32 |
| sint64 | Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s. | int64 | long | int/long[3] | *int64 |
| fixed32 | Always four bytes. More efficient than uint32 if values are often greater than 228. | uint32 | int[1] | int/long[3] | *uint32 |
| fixed64 | Always eight bytes. More efficient than uint64 if values are often greater than 256. | uint64 | long[1] | int/long[3] | *uint64 |
| sfixed32 | Always four bytes. | int32 | int | int | *int32 |
| sfixed64 | Always eight bytes. | int64 | long | int/long[3] | *int64 |
| bool | bool | boolean | bool | *bool | |
| string | A string must always contain UTF-8 encoded or 7-bit ASCII text. | string | String | unicode (Python 2) or str (Python 3) | *string |
| bytes | May contain any arbitrary sequence of bytes. | string | ByteString | bytes | []byte |
V3的用法
1、NET4.5的项目,安装Nuget包<Google.Protobuf>和<Google.Protobuf.Tools>
2、编写proto文件,类型参照上文定义;放一个demo.proto
syntax = "proto3";
package protobuf_unittest_import;
option csharp_namespace = "Export.DAL";
message ListFloat
{
repeated float items =1;
}
message CircleInfoBuf
{
int32 No =1;
string Name =2;
float Diameter =3;
float Length =4;
ListFloat Points =5;
}
3、运行\packages\Google.Protobuf.Tools.3.9.1\tools\windows_x64下的protoc.exe,执行命令,转成cs代码;
protoc.exe --proto_path=所在目录 --csharp_out=生成的目录 demo.proto
4、引用此cs文件,编写其他代码
5、序列化和反序列化
序列化
string bufFile = "";
using (FileStream fs = new FileStream(bufFile, FileMode.CreateNew))
{
using (CodedOutputStream cos = new CodedOutputStream(fs))
{
list.WriteTo(cos);//需序列化的对象
fs.Flush();
}
}
反序列化
using (FileStream fs = new FileStream(bufFile, FileMode.Open))
{
using (CodedInputStream cos = new CodedInputStream(fs))
{
CircleInfoBuf list = CircleInfoBuf.Parser.ParseFrom(cos);
return list;
}
}
P2:protobuf-net
1、定义类