Dameng.Protobuf.Extension 0.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Dameng.Protobuf.Extension --version 0.1.0
                    
NuGet\Install-Package Dameng.Protobuf.Extension -Version 0.1.0
                    
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.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Dameng.Protobuf.Extension" Version="0.1.0" />
                    
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.1.0
                    
#r "nuget: Dameng.Protobuf.Extension, 0.1.0"
                    
#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.1.0
                    
#: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.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Dameng.Protobuf.Extension&version=0.1.0
                    
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.

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.

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

Installation

Install both packages from NuGet:

dotnet add package Dameng.Protobuf.Extension
dotnet add package Dameng.Protobuf.Extension.Generator

Usage

1. Configure Your Protobuf Files

In your .csproj file, ensure your protobuf files have the MSBuild:PreCompile generator:

<ItemGroup>
  <ProtoBuf Include="your-proto-file.proto">
    <GrpcServices>Client</GrpcServices>
    <Generator>MSBuild:PreCompile</Generator>
  </ProtoBuf>
</ItemGroup>

2. Write Generic Deserialization Methods

Once configured, you can write generic methods that work with any protobuf message type:

using Dameng.Protobuf.Extension;
using Google.Protobuf;

// Generic deserialization method
T Deserialize<T>(byte[] bytes) where T : IPbMessageParser<T>, IMessage<T>
{
    return T.Parser.ParseFrom(bytes);
}

// Usage with any protobuf generated class
var request = Deserialize<MyRequest>(requestBytes);
var response = Deserialize<MyResponse>(responseBytes);

3. What the Source Generator Does

The source generator automatically adds the IPbMessageParser<TSelf> interface to all your protobuf generated classes:

// Generated code (automatic)
using Google.Protobuf;
using Dameng.Protobuf.Extension;

namespace YourNamespace
{
    partial class MyRequest : IPbMessageParser<MyRequest> {}
}

namespace YourNamespace  
{
    partial class MyResponse : IPbMessageParser<MyResponse> {}
}

Interface Definition

The core interface is simple and leverages C# generic static interface members:

namespace Dameng.Protobuf.Extension;

/// <summary>
/// Interface that enables generic protobuf message parsing by exposing the static Parser property.
/// This interface is automatically implemented by the source generator for all protobuf generated classes.
/// </summary>
/// <typeparam name="TSelf">The protobuf message type that implements this interface</typeparam>
public interface IPbMessageParser<TSelf> where TSelf : IPbMessageParser<TSelf>, IMessage<TSelf>
{
    /// <summary>
    /// Gets the static parser instance for the protobuf message type.
    /// This property is automatically available on all protobuf generated classes.
    /// </summary>
    public static abstract Google.Protobuf.MessageParser<TSelf> Parser { get; }
}

Example Scenario

Consider you're building a generic TCP communication library that needs to deserialize different message types:

public class GenericProtobufHandler
{
    public T HandleMessage<T>(byte[] messageBytes) 
        where T : IPbMessageParser<T>, IMessage<T>
    {
        // Generic deserialization - works with any protobuf message
        return T.Parser.ParseFrom(messageBytes);
    }
    
    public byte[] SerializeMessage<T>(T message) 
        where T : IPbMessageParser<T>, IMessage<T>
    {
        return message.ToByteArray();
    }
}

// Usage
var handler = new GenericProtobufHandler();
var request = handler.HandleMessage<MyRequest>(requestBytes);
var response = handler.HandleMessage<MyResponse>(responseBytes);

Requirements

  • .NET 8.0 or higher
  • Google.Protobuf package
  • C# 11.0 or higher (for generic static interface members)

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 114 9/5/2025
0.2.6 114 9/5/2025
0.2.4 115 9/5/2025
0.2.3 122 9/5/2025
0.2.2 131 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 81 8/22/2025