Skip to content

.NET¤

This example shows how to access a 32-bit .NET library from 64-bit Python (Windows only — Mono can load both 32-bit and 64-bit libraries on 64-bit Linux and therefore a 32-bit .NET library can be loaded directly via LoadLibrary on 64-bit Linux). DotNet32 is the 32-bit server and DotNet64 is the 64-bit client. The source code of the C# program is available here.

Decompile a .NET assembly

The JetBrains dotPeek program can be used to decompile a .NET assembly. For example, peeking inside the example dotnet_lib32.dll library, that the DotNet32 class is a wrapper around, gives

dotpeek_lib.png

Configure a .NET runtime

To configure pythonnet to use the .NET Core runtime, you must either run

from pythonnet import load
load("coreclr")

or define a PYTHONNET_RUNTIME=coreclr environment variable, e.g.,

import os
os.environ["PYTHONNET_RUNTIME"] = "coreclr"

before super() is called in the Server32 subclass. To use the Mono runtime, replace "coreclr" with "mono".

Create a DotNet64 client to communicate with the 32-bit dotnet_lib32.dll library

>>> from msl.examples.loadlib import DotNet64
>>> dn = DotNet64()

Numeric types¤

Add two integers, see DotNet64.add_integers

>>> dn.add_integers(8, 2)
10

Divide two C# floating-point numbers, see DotNet64.divide_floats

>>> dn.divide_floats(3., 2.)
1.5

Multiple two C# double-precision numbers, see DotNet64.multiply_doubles

>>> dn.multiply_doubles(872.24, 525.525)
458383.926

Add or subtract two C# double-precision numbers, see DotNet64.add_or_subtract

>>> dn.add_or_subtract(99., 9., do_addition=True)
108.0
>>> dn.add_or_subtract(99., 9., do_addition=False)
90.0

Arrays¤

Multiply a 1D array by a number, see DotNet64.scalar_multiply

>>> a = [float(val) for val in range(10)]
>>> a
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
>>> dn.scalar_multiply(2.0, a)
[0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0]

Multiply two matrices, see DotNet64.multiply_matrices

>>> m1 = [[1., 2., 3.], [4., 5., 6.]]
>>> m2 = [[1., 2.], [3., 4.], [5., 6.]]
>>> dn.multiply_matrices(m1, m2)
[[22.0, 28.0], [49.0, 64.0]]

Strings¤

Get the names of the classes in the .NET library module, see DotNet64.get_class_names

>>> dn.get_class_names()
['StringManipulation', 'StaticClass', 'DotNetMSL.BasicMath', 'DotNetMSL.ArrayManipulation']

Reverse a string, see DotNet64.reverse_string

>>> dn.reverse_string("New Zealand")
'dnalaeZ weN'

Static Class¤

Call the static methods in the StaticClass class

>>> dn.add_multiple(1, 2, 3, 4, 5)
15
>>> dn.concatenate("the", " experiment", " worked", False, " temporarily")
'the experiment worked'
>>> dn.concatenate("the", " experiment", " worked", True, " temporarily")
'the experiment worked temporarily'

You have access to the server's stdout and stderr streams when you shut down the server

>>> stdout, stderr = dn.shutdown_server32()