Convert C# to Python

Convert a .NET assembly or NuGet package into a Python extension module in the WHL format, containing all the members of your C# library. These members can be used for Python programming in the same manner as elements of the native Python type system or any other Python extensions. Any generated Python module is compatible to Linux, MacOS, and Windows.

CodePorting.Wrapper Cs2Python is the best way to integrate .NET into Python projects

CodePorting.Wrapper Cs2Python is a tool designed to make .NET-based software products usable in Python via wrapping them. This tool is especially useful for developers who want to use their existing .NET code in Python projects without having to rewrite the entire codebase. The wrapper allows them to easily integrate their .NET components into Python applications, thus saving time and effort.

Hot News

Cs2Python Reaches Milestone: 25 Products Successfully Wrapped from C# to Python

C# example
CSharp code example
Calling a C# method from Python
Example using wrapped CSharp code in Python

Using C# types and methods in Python programming

When using wrapped C# code in Python, you can create objects of types declared in the wrapped C# assembly. These objects can be instantiated and manipulated just like any other Python objects. You can call methods and access properties on these objects. Additionally, these objects can be passed as arguments to methods, allowing for seamless integration between the Python and C# code.

By combining the two languages, you can harness the power and flexibility of C# while enjoying the simplicity and ease of use of Python. This is especially useful when you need specific features or libraries that are only available in C#.

Additionally, this can be very useful if you are creating a large library in C# and want to distribute it to Python users as a Wheel package. By wrapping the C# assembly and providing a Python interface, you can make your library easily accessible and usable for Python developers. This allows for seamless integration of your C# library into Python projects, expanding its reach and potential user base. The ability to distribute your C# library as a Python Wheel package makes it convenient for Python users to install and utilize your library, further enhancing its accessibility and usability.

Creating a Python extension module

The source of the wrapper is a C# library compiled into a NuGet package. The output is a Python Wheel package, which is considered the most standard package format for distributing Python extensions. A significant advantage is that the wrapped C# assembly can have dependencies on any third-party NuGet packages. Additionally, it is not necessary for the Python user's system to have the .NET Framework installed. This provides greater flexibility and ease of use, as users can seamlessly integrate your C# library into their Python projects without any additional installations or dependencies.
csharp to python conversion scheme
C# code
Csharp code example 2
Python code
Python code example 2

Interaction of Python code with wrapped C# code

The example shows how collections and enumerations declared in a wrapped C# assembly can be used in Python code. Implicit conversion is supported not only between base types, but also between Python and C# collections. This means that data can be easily transferred between the two languages without the need for complex data transformation. This is possible because wrapper-module includes a code for implicit conversions between .NET system types and their corresponding native Python types for passing their values between .NET and Python contexts.

Python protocols are also supported, so wrapped C# types can be used in Python compound statements such as for, with, in, etc. This allows for easy iteration and resource management in Python code that interacts with the wrapped C# objects. Moreover, wrapped types for arrays and collections support Python slice notation, making it convenient to access and manipulate elements within these data structures.

In summary, CodePorting.Wrapper Cs2Python offers a wide range of capabilities, including creating objects of C# types, working with enums, invoking methods, accessing properties, passing objects as method arguments, supporting collections, integrating with various Python protocols, and more. This powerful tool allows developers to create robust and flexible applications by harnessing the strengths of both C# and Python.

Examples of C# products successfully wrapped for Python

Related Articles