⚠ Deprecated: Legacy
Suggested alternative: LightProto
A .NET library that enables generic protobuf deserialization by providing an interface and source generator for Google.Protobuf generated classes. Allows writing type-safe generic methods for protobuf message parsing.
$ dotnet add package Dameng.Protobuf.ExtensionA .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.
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.
Install packages from NuGet:
dotnet add package Dameng.Protobuf.Extension
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);
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:
protobuf-net with Google.Protobuf + this extension.proto files:
// Standard .proto file
syntax = "proto3";
message MyMessage {
string name = 1;
}
where T : class, new() to where 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 |
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> {}
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();
}
}
<PublishAot>true</PublishAot>MIT License - see LICENSE file for details.
Contributions are welcome! Please feel free to submit issues and pull requests.