Protobuf.CodeGeneration
1.0.2
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Protobuf.CodeGeneration --version 1.0.2
NuGet\Install-Package Protobuf.CodeGeneration -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Protobuf.CodeGeneration" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Protobuf.CodeGeneration" Version="1.0.2" />
<PackageReference Include="Protobuf.CodeGeneration" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Protobuf.CodeGeneration --version 1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Protobuf.CodeGeneration, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Protobuf.CodeGeneration@1.0.2
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Protobuf.CodeGeneration&version=1.0.2
#tool nuget:?package=Protobuf.CodeGeneration&version=1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Protobuf Library - 轻量级 Protobuf3 库
一个完全独立的、支持 AOT 的 Protobuf3 库,无需依赖 Google.Protobuf 或 protobuf-net。
特性
- ✅ 零依赖:不依赖任何外部 Protobuf 库
- ✅ AOT 友好:完全支持 NativeAOT 编译
- ✅ Protobuf3 支持:完整的 protobuf3 标准支持
- ✅ Source Generator:编译时自动生成代码
- ✅ CSProj 集成:支持通配符匹配,
**/*.proto - ✅ hasXxx() 方法:检查字段是否有值
- ✅ 完整类型支持:基础类型、repeated、oneof、map
- ✅ 构建系统集成:无缝集成到 MSBuild
支持的类型
基础类型
double,floatint32,int64,uint32,uint64sint32,sint64(ZigZag 编码)fixed32,fixed64,sfixed32,sfixed64bool,string,bytes
高级类型
repeated- 列表字段oneof- 互斥字段map<K,V>- 字典字段- 嵌套消息
- 枚举
快速开始
1. 定义 .proto 文件
syntax = "proto3";
package Example;
message Person {
string name = 1;
int32 id = 2;
string email = 3;
repeated string phone_numbers = 4;
oneof address {
string home_address = 5;
string work_address = 6;
}
map<string, string> metadata = 7;
}
2. 配置项目文件
<ItemGroup>
<ProjectReference Include="Protobuf.Core\Protobuf.Core.csproj" />
<ProjectReference Include="Protobuf.Generator\Protobuf.Generator.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
<ProtoFile Include="Protos\**\*.proto" />
</ItemGroup>
3. 使用生成的代码
var person = new Person();
// 设置字段
person.SetName("Alice");
person.SetId(12345);
// 检查字段是否有值
if (person.HasName)
{
Console.WriteLine($"Name: {person.Name}");
}
// 使用 repeated 字段
person.AddPhoneNumbers("555-1234");
person.AddPhoneNumbers("555-5678");
// 使用 oneof
person.SetHomeAddress("123 Home St");
// 切换到另一个 oneof 选项(会自动清除前一个)
person.SetWorkAddress("456 Work Ave");
// 使用 map
person.AddMetadata("department", "Engineering");
API 参考
hasXxx() 方法
每个字段都有一个对应的 HasXxx() 方法,用于检查字段是否有值:
person.HasName // bool
person.HasId // bool
person.HasEmail // bool
person.HasPhoneNumbers // bool
Setter 方法
使用 SetXxx() 方法设置字段值:
person.SetName("Alice");
person.SetId(12345);
person.SetEmail("alice@example.com");
Getter 属性
使用 Xxx 属性获取字段值:
string name = person.Name;
int id = person.Id;
Repeated 字段
对于 repeated 字段,使用 AddXxx() 方法:
person.AddPhoneNumbers("555-1234");
// 获取只读列表
IReadOnlyList<string> phones = person.PhoneNumbers;
Oneof 字段
person.SetHomeAddress("123 Home St");
// 检查哪个 oneof 字段被设置
if (person.HasHomeAddress)
{
Console.WriteLine($"Home: {person.HomeAddress}");
}
// 获取 oneof case
Person.AddressCase addrCase = person.AddressCase;
// 清除 oneof
person.ClearAddress();
Map 字段
person.AddMetadata("key", "value");
// 获取只读字典
IReadOnlyDictionary<string, string> metadata = person.Metadata;
序列化
// 序列化到字节数组
byte[] data = person.Serialize();
// 从字节数组反序列化
Person person2 = Person.Parser.ParseFrom(data);
JSON 支持
// 序列化到 JSON(PascalCase)
string json = person.ToJson();
// {"Name":"Alice","Id":12345,...}
// 从 JSON 反序列化
Person person3 = Person.Parser.ParseJson(json);
项目结构
Protobuf.Library/
├── src/
│ ├── Protobuf.Core/ # 核心运行时库
│ ├── Protobuf.Parser/ # .proto 文件解析器
│ ├── Protobuf.Generator/ # Source Generator
│ ├── Protobuf.Json/ # JSON 支持
│ └── Protobuf.Reflection/ # 反射 API
├── tests/
│ └── Protobuf.Tests/
└── examples/
└── Examples/
🚀 如何使用
快速开始
查看 QUICKSTART.md 了解一分钟上手指南
详细指南
查看 USAGE.md 获取完整使用文档
运行示例
cd examples/Examples
dotnet run -- parser # Proto 文件解析示例
dotnet run -- codec # 编解码器使用示例
dotnet run -- manual # 手动消息处理示例
dotnet run -- realworld # 真实应用场景示例
当前使用方式
阶段一:手动方式(当前可用)
- ✅ 解析 .proto 文件了解结构
- ✅ 使用运行时 API 手动编码/解码
- ⏳ 等待 Source Generator
阶段二:自动生成(即将推出)
- ⏳ 定义 .proto 文件
- ⏳ Source Generator 自动生成 C# 代码
- ⏳ 开箱即用,无需手写代码
与 Google.Protobuf 和 protobuf-net 的区别
Google.Protobuf
- ❌ 需要额外的 protoc 工具生成代码
- ❌ 生成的代码冗长且难以阅读
- ❌ 大型依赖
protobuf-net
- ❌ 主要依赖运行时反射
- ❌ AOT 不支持
- ❌ 配置复杂
本库
- ✅ Source Generator 自动生成代码
- ✅ 生成的代码简洁易读
- ✅ 完全支持 AOT
- ✅ 零外部依赖
- ✅ 单一库,开箱即用
开发状态
- 核心运行时(Wire Format、编解码器)
- Proto 文件解析器
- 代码生成器(Source Generator)
- 基础类型支持
- Repeated 字段支持
- OneOf 字段支持
- Map 字段支持(完整序列化/反序列化)
- 嵌套消息类型支持
- JSON 序列化/反序列化(PascalCase)
- AOT 友好的反射 API
- 所有项目成功构建
- 完整的序列化/反序列化实现
- 单元测试(54个测试全部通过)
- 文档完善
许可证
待定
贡献
欢迎贡献!
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.1
- Protobuf.Core (>= 1.0.2)
- Protobuf.Parser (>= 1.0.2)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Protobuf.CodeGeneration:
| Package | Downloads |
|---|---|
|
Protobuf.Generator.Tasks
MSBuild tasks for Protobuf code generation |
GitHub repositories
This package is not used by any popular GitHub repositories.