Skip to content

connorivy/DotWrap

Repository files navigation

DotWrap

✨ What is DotWrap?

DotWrap is a tool that automatically generates Python wrappers for your .NET AOT projects.
Write your logic once in C#, publish with DotWrap, and you’ll get a ready-to-install Python package that feels completely native.

The generated Python package includes docstrings, type hints, and IntelliSense support, so you can consume your C# code in Python as if it were written there.


🛠 Basic Usage

Suppose you have a C# class you want to expose to Python:

using DotWrap;

namespace CoolCalc;

/// <summary>
/// Custom summary for cool calc calculator.
/// </summary>
[DotWrapExpose] // <-- mark with attribute for source generator discoverability
public class Calculator
{
    /// <summary>
    /// Adds two integers together.
    /// </summary>
    /// <param name="a">The first integer to add.</param>
    /// <param name="b">The second integer to add.</param>
    /// <returns>The sum of the two integers.</returns>
    public int Add(int a, int b) => a + b;
}

After you mark the classes you want to expose with the DotWrapExpose attribute, build and publish your project with:

dotnet publish -r linux-x64 # or win-x64, osx-arm64, etc.

DotWrap will automatically generate a Python package inside python-package-root, complete with docstrings and type hints:

# main.py (auto-generated by DotWrap)

class Calculator:
"""
Custom summary for cool calc calculator.
"""
def add(self, a: int, b: int) -> int:
    """
    Adds two integers together.

    :param a: The first integer to add.
    :param b: The second integer to add.
    :return: The sum of the two integers.
    """
    # implementation skipped for brevity

You can install and test it locally:

cd ./python-package-root
pip install .

Now use your C# code seamlessly from Python:

import cool_calc

calc = cool_calc.Calculator()
print(calc.add(2, 3)) # Output: 5

Read the Wiki for more examples and documentation

📦 Features of the Generated Python Package

When you wrap your .NET project with DotWrap, the resulting Python package has some important advantages:

  • No .NET runtime required – the wrapper is based on your AOT-compiled library, so end users only need Python.
  • Native speed – the wrapper is implemented as a CPython extension module for extremely low overhead.
  • Full type hints and docstrings – IntelliSense and autocomplete work out of the box.
  • Microsecond overhead – only ~0.3µs per call across the Python ↔ C# boundary.

⭐️ If you like DotWrap, please give the project a star on GitHub! ⭐️

GitHub stars

⚡ Performance

While DotWrap is primarily about portability and usability, it also adds almost no overhead when bridging Python and C#.

Our benchmarks show that just calling into C# and returning takes about 0.000000344 seconds (0.344 µs)—fast enough that you won’t notice the boundary between Python and C#.

And when you offload real work to C#, the performance gains can be dramatic. For example, these are the results of the sum primes benchmarks which sums all primes numbers below 500,000:

Implementation Result Time
Python 3.13 9914236195 0.786 s
C# with .NET 10 (via DotWrap) 9914236195 0.036 s

C# was 22x faster than Python in this benchmark.


📦 Summary

  • Wrap your .NET AOT projects – DotWrap turns them into Python packages.
  • No runtime dependency – Generated packages are AOT-compiled, no .NET runtime needed.
  • Ease of use – Auto-generated packages with docstrings and type hints.
  • Native integration – CPython extension modules for low-overhead calls.
  • Performance – Just 0.3µs overhead per call, plus C# speed where it matters.

This project is licensed under the MIT License. See the LICENSE file for details.

About

Automatically generate a python package from c# aot project

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages