.NET¤
Load a 64-bit C# library (a .NET Framework) in 64-bit Python. 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"
as the filename.
Example¤
Load the example .NET library
Tip
"clr"
is an alias for "net"
and can also be used as the value of libtype
when instantiating LoadLibrary.
>>> from msl.loadlib import LoadLibrary
>>> from msl.examples.loadlib import EXAMPLES_DIR
>>> net = LoadLibrary(EXAMPLES_DIR / "dotnet_lib64.dll", "net")
The 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]
Call the reverse_string
method in the StringManipulation
class to reverse a string
>>> net.lib.StringManipulation().reverse_string("abcdefghijklmnopqrstuvwxyz")
'zyxwvutsrqponmlkjihgfedcba'
Call 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]
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 LoadLibrary is called. To use the Mono runtime, replace "coreclr"
with "mono"
.
.NET Source Code¤
dotnet_lib.cs
// dotnet_lib.cs
// Examples that show how to pass various data types between Python and a C# library.
//
using System;
// The DotNetMSL namespace contains two classes: BasicMath, ArrayManipulation
namespace DotNetMSL
{
// A class that is part of the DotNetMSL namespace
public class BasicMath
{
public int add_integers(int a, int b)
{
return a + b;
}
public float divide_floats(float a, float b)
{
return a / b;
}
public double multiply_doubles(double a, double b)
{
return a * b;
}
public double add_or_subtract(double a, double b, bool do_addition)
{
if (do_addition)
{
return a + b;
}
else
{
return a - b;
}
}
}
// A class that is part of the DotNetMSL namespace
public class ArrayManipulation
{
public double[] scalar_multiply(double a, double[] xin)
{
int n = xin.GetLength(0);
double[] xout = new double[n];
for (int i = 0; i < n; i++)
{
xout[i] = a * xin[i];
}
return xout;
}
public double[,] multiply_matrices(double[,] A, double[,] B)
{
int rA = A.GetLength(0);
int cA = A.GetLength(1);
int rB = B.GetLength(0);
int cB = B.GetLength(1);
double temp = 0;
double[,] C = new double[rA, cB];
if (cA != rB)
{
Console.WriteLine("matrices can't be multiplied!");
return new double[0, 0];
}
else
{
for (int i = 0; i < rA; i++)
{
for (int j = 0; j < cB; j++)
{
temp = 0;
for (int k = 0; k < cA; k++)
{
temp += A[i, k] * B[k, j];
}
C[i, j] = temp;
}
}
return C;
}
}
}
}
// A class that is not part of the DotNetMSL namespace
public class StringManipulation
{
public string reverse_string(string original)
{
char[] charArray = original.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}
// A static class
public static class StaticClass
{
public static int add_multiple(int a, int b, int c, int d, int e)
{
return a + b + c + d + e;
}
public static string concatenate(string a, string b, string c, bool d, string e)
{
string res = a + b + c;
if (d)
{
res += e;
}
return res;
}
}