Dameng.Protobuf.Extension
0.2.7
dotnet add package Dameng.Protobuf.Extension --version 0.2.7
NuGet\Install-Package Dameng.Protobuf.Extension -Version 0.2.7
<PackageReference Include="Dameng.Protobuf.Extension" Version="0.2.7" />
<PackageVersion Include="Dameng.Protobuf.Extension" Version="0.2.7" />
<PackageReference Include="Dameng.Protobuf.Extension" />
paket add Dameng.Protobuf.Extension --version 0.2.7
#r "nuget: Dameng.Protobuf.Extension, 0.2.7"
#:package Dameng.Protobuf.Extension@0.2.7
#addin nuget:?package=Dameng.Protobuf.Extension&version=0.2.7
#tool nuget:?package=Dameng.Protobuf.Extension&version=0.2.7
Dameng.Protobuf.Extension
A .NET library that enables generic protobuf deserialization by providing an interface and source generator for Google.Protobuf generated classes. Designed as a NativeAOT-compatible alternative to protobuf-net for applications requiring ahead-of-time compilation.
Overview
This library allows you to write generic deserialization functions for protobuf messages without knowing the specific type at compile time. It achieves this by adding a IPbMessageParser<TSelf>
interface to protobuf generated classes through source generation.
Why Choose This Over protobuf-net?
This library is specifically designed for NativeAOT compatibility, making it an ideal replacement for protobuf-net in scenarios where ahead-of-time compilation is required. Unlike protobuf-net, which relies heavily on reflection and runtime code generation, this solution uses source generation to provide the same generic capabilities with full NativeAOT support.
Features
- Generic Deserialization: Write type-safe generic methods for protobuf deserialization
- Source Generator: Automatically implements the interface for all protobuf generated classes
- Zero Runtime Overhead: All code generation happens at compile time
- Easy Integration: Just add the NuGet packages and configure your protobuf files
- NativeAOT Compatible: Full support for ahead-of-time compilation scenarios
- protobuf-net Migration: Smooth transition path from protobuf-net for NativeAOT projects
Installation
Install packages from NuGet:
dotnet add package Dameng.Protobuf.Extension
Migration & Usage
Quick Start
Add the required packages:
dotnet add package Dameng.Protobuf.Extension
Configure your .csproj
with protobuf files:
<ItemGroup>
<ProtoBuf Include="your-proto-file.proto">
<GrpcServices>Client</GrpcServices>
<Generator>MSBuild:PreCompile</Generator>
</ProtoBuf>
</ItemGroup>
Write generic methods that work with any protobuf message:
using Dameng.Protobuf.Extension;
using Google.Protobuf;
// Generic deserialization
T Deserialize<T>(byte[] bytes) where T : IPbMessageParser<T>, IMessage<T>
{
return T.Parser.ParseFrom(bytes);
}
// Generic serialization
byte[] Serialize<T>(T message) where T : IPbMessageParser<T>, IMessage<T>
{
return message.ToByteArray();
}
// Usage with any protobuf generated class
var request = Deserialize<MyRequest>(requestBytes);
var response = Deserialize<MyResponse>(responseBytes);
Migrating from protobuf-net
Why migrate? protobuf-net has limited NativeAOT support due to heavy reflection usage, while this library provides full compatibility through compile-time source generation.
Migration steps:
- Replace
protobuf-net
withGoogle.Protobuf
+ this extension - Convert protobuf-net attributes to standard
.proto
files:// Standard .proto file syntax = "proto3"; message MyMessage { string name = 1; }
- Update generic code constraints from
where T : class, new()
towhere T : IPbMessageParser<T>, IMessage<T>
Key differences:
Aspect | protobuf-net | This Extension |
---|---|---|
NativeAOT | Limited | Full support |
Code Generation | Runtime | Compile-time |
Reflection | Heavy usage | None |
How It Works
The source generator automatically implements IPbMessageParser<TSelf>
on all protobuf generated classes, exposing their static Parser
property for generic access:
// Auto-generated for each protobuf class
partial class MyRequest : IPbMessageParser<MyRequest> {}
Example: Generic Communication Handler
public class GenericProtobufHandler
{
public T HandleMessage<T>(byte[] messageBytes)
where T : IPbMessageParser<T>, IMessage<T>
{
return T.Parser.ParseFrom(messageBytes);
}
public byte[] SerializeMessage<T>(T message)
where T : IPbMessageParser<T>, IMessage<T>
{
return message.ToByteArray();
}
}
Requirements
- .NET 8.0 or higher
- Google.Protobuf package
- C# 11.0 or higher (for generic static interface members)
- NativeAOT Compatible: Full support for ahead-of-time compilation when using
<PublishAot>true</PublishAot>
License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit issues and pull requests.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- Google.Protobuf (>= 3.32.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.