Microsoft .NET
Load a 64-bit C# library (a .NET Framework) in 64-bit Python (view the
C# source code). Include the 'net' argument to
indicate that the .dll file is for the .NET Framework.
To load the 32-bit library in 32-bit Python use '/dotnet_lib32.dll'.
>>> from msl.loadlib import LoadLibrary
>>> from msl.examples.loadlib import EXAMPLES_DIR
>>> net = LoadLibrary(EXAMPLES_DIR + '/dotnet_lib64.dll', 'net')
Tip
'clr' is an alias for 'net' and can also be used as the libtype
Attention
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
import os
os.environ["PYTHONNET_RUNTIME"] = "coreclr"
before LoadLibrary is called.
To use the Mono runtime, replace "coreclr" with "mono".
The dotnet_lib64 library contains a reference to the
DotNetMSL module (which is a C# namespace), the StaticClass class,
the StringManipulation class and the System namespace
Create an instance of the BasicMath class in the DotNetMSL namespace
and call the multiply_doubles method
>>> bm = net.lib.DotNetMSL.BasicMath()
>>> bm.multiply_doubles(2.3, 5.6)
12.879999...
Create an instance of the ArrayManipulation class in the DotNetMSL
namespace and call the scalar_multiply method
>>> am = net.lib.DotNetMSL.ArrayManipulation()
>>> values = am.scalar_multiply(2., [1., 2., 3., 4., 5.])
>>> values
<System.Double[] object at ...>
>>> [val for val in values]
[2.0, 4.0, 6.0, 8.0, 10.0]
Use the reverse_string method in the StringManipulation class to
reverse a string
>>> net.lib.StringManipulation().reverse_string('abcdefghijklmnopqrstuvwxyz')
'zyxwvutsrqponmlkjihgfedcba'
Use the static add_multiple method in the StaticClass class to add
five integers
>>> net.lib.StaticClass.add_multiple(1, 2, 3, 4, 5)
15
One can create objects from the System namespace,
>>> System = net.lib.System
for example, to create a 32-bit signed integer,
>>> System.Int32(9)
<System.Int32 object at ...>
or, a one-dimensional Array of the specified Type
>>> array = System.Array[int](list(range(10)))
>>> array
<System.Int32[] object at ...>
>>> list(array)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> array[0] = -1
>>> list(array)
[-1, 1, 2, 3, 4, 5, 6, 7, 8, 9]