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
                    
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="Dameng.Protobuf.Extension" Version="0.2.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Dameng.Protobuf.Extension" Version="0.2.7" />
                    
Directory.Packages.props
<PackageReference Include="Dameng.Protobuf.Extension" />
                    
Project file
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 Dameng.Protobuf.Extension --version 0.2.7
                    
#r "nuget: Dameng.Protobuf.Extension, 0.2.7"
                    
#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 Dameng.Protobuf.Extension@0.2.7
                    
#: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=Dameng.Protobuf.Extension&version=0.2.7
                    
Install as a Cake Addin
#tool nuget:?package=Dameng.Protobuf.Extension&version=0.2.7
                    
Install as a Cake Tool

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:

  1. Replace protobuf-net with Google.Protobuf + this extension
  2. Convert protobuf-net attributes to standard .proto files:
    // Standard .proto file
    syntax = "proto3";
    message MyMessage {
      string name = 1;
    }
    
  3. Update generic code constraints from 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

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.7 111 9/5/2025
0.2.6 113 9/5/2025
0.2.4 114 9/5/2025
0.2.3 122 9/5/2025
0.2.2 130 9/5/2025
0.2.1 138 9/4/2025
0.1.4 130 9/1/2025
0.1.2 190 8/26/2025
0.1.1 188 8/26/2025
0.1.0 80 8/22/2025