Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Direct .NET in Python | Use C# classes in Python seamlessly

License

Notifications You must be signed in to change notification settings

sadhbh-c0d3/pydotnet

Repository files navigation

$${\color{#3c6491}(Py)}$$ dotnet $${\color{gray}|}$$ $${\color{#3c6491}Py\color{yellow}.\color{#693c91}NET}$$

$${\color{red}IMPORTANT: \space \color{#cc4b5f}The \space PyPI \space module \space name \space is \space \color{text} dotnet \space \color{red} and \space not \color{#e6932e} pydotnet}$$

    pip install dotnet
  • $${\color{gray}PIP \space module \space name:}$$ dotnet
  • $${\color{gray}Repository \space name:}$$ PyDotnet
  • $${\color{gray}Open \space Source \space Release \space:}$$ 2016 on BitBucket
  • $${\color{gray}Moved \space to \space GitHub \space in:}$$ March 2021

Watch My Video! Watch My Video!

About

Direct interop between Python and .NET assemblies via Microsoft C++/CLR and Boost.Python.

This is regular module for native Python (CPython), and it uses .NET runtime library (mscoree.dll) to natively support .NET assemblies.

No annotations required! You can load .NET assemblies just like that!

  • Load into Python any .NET assembly
  • Build .NET assembly directly in Python - no additional tools required!
  • Experiment with .NET libraries
  • Automate testing of .NET projects
  • Build control panels using powerful .NET libraries, and flexibility of Python

Watch on Loom

Compatibility

Windows

PyDotnet only works on Windows due to integration with Microsoft .NET Framework (.NetCore C++/CLI for Linux and MacOS - Is not supported)

Works natively on Windows 64-Bit (x86), and also on Windows on ARM64 in AMD64 emulation mode.

Mac

Works well on Windows on ARM64 on Apple Silicon: Parallels® Desktop 18 for Mac

Linux

Haven't tested, but this might work: Windows-Docker-Machine

Stay tuned! More to come...

Usage

Example

    import dotnet.seamless
    import System
    
    from System.Collections.Generic import List
    
    lst = List[Int32]()
    
    lst.Add(1)
    lst.Add(2)
    lst.Add(3)
    
    lst.AddRange([4,5,6])
    
    lst.FindIndex(lambda x: x > 3)

Explanation

Import .NET seamless integration for CPython shell

    import dotnet.seamless

Import .NET namespace

    import System

Import type from .NET namespace

    from System.Collections.Generic import List

Create an instance of .NET type. This shows also how to specialize .NET generic type

    lst = List[Int32]()

Call instance method of .NET type. This also converts Python int into .NET Int32

    lst.Add(1)
    lst.Add(2)
    lst.Add(3)

Call instance method of .NET type. This also converts Python list into .NET IEnumerable

    lst.AddRange([4,5,6])

Call instance method of .NET type. Here Python lambda gets converted into .NET Predicate. The invocation jumps from Python into .NET CLR and calls back to Python lambda

    lst.FindIndex(lambda x: x > 3)

Installation

    pip install dotnet

Documentation

See https://github.com/sadhbh-c0d3/pydotnet/blob/main/SLIDES.md

Building

See https://github.com/sadhbh-c0d3/pydotnet/blob/main/BUILD.md