Knapcode.ProtoRebuilder 0.4.0

Prefix Reserved
dotnet tool install --global Knapcode.ProtoRebuilder --version 0.4.0
                    
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest
                    
if you are setting up this repo
dotnet tool install --local Knapcode.ProtoRebuilder --version 0.4.0
                    
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=Knapcode.ProtoRebuilder&version=0.4.0
                    
nuke :add-package Knapcode.ProtoRebuilder --version 0.4.0
                    

ProtoRebuilder

Generate .proto files from .NET assemblies.

This is a .NET tool that scans .NET assemblies for types that implement IMessage and generates corresponding .proto files.

The idea is that you used protoc (either directly or via Grpc.Tools) to generate some C# from .proto files. This was compiled into an assembly. Then you lost the .proto files! What do you do? You can generate .proto files from the assembly using this tool.

Maybe in the future, I will enhance the tool to also generate .proto files from any group of .NET types (perhaps as a way to transition from one wire format to another), but for now, it only works with assemblies containing Google.Protobuf.IMessage types.

Usage

Install the .NET SDK from https://get.dot.net/. This allows you to run .NET tool packages easily.

With .NET SDK 10+, you can use dnx:

dnx Knapcode.ProtoRebuilder path/to/assembly.dll dir/for/protos

With .NET SDK 8+, you can use dotnet tool install:

dotnet tool install --global Knapcode.ProtoRebuilder

Then run the tool with:

protorebuilder path/to/assembly.dll dir/for/protos

This will scan the assembly (first argument) for types that implement Google.Protobuf.IMessage and generate .proto files in the specified directory (second argument). If the IMessage types are in different namespaces, multiple proto files will be generated, one for each namespace. Only enums used by the IMessage types will be included in the generated .proto files.

Take a look at the test data for examples of the input and output .proto files. Here is a sample:

Caveats

This is not a perfect round-trip (proto → .NET assembly → proto). Here is a list of problems I know about:

  • Enum names may change. The values (which are what go over the wire) should stay the same.
  • The well-known type StringValue is assumed to be a string (no wrapper). This can cause deserialization issues because the string value is actually wrapped in a message with a single value field, but the generated .proto files only know about the inner string (no wrapper).
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.

This package has no dependencies.

Version Downloads Last Updated
0.4.0 217 8/27/2025
0.3.0 297 8/25/2025
0.2.0 270 8/25/2025
0.1.0 278 8/25/2025