Skip to content

DotNet32¤

Wrapper around a 32-bit .NET library.

Example of a server that loads a 32-bit library, dotnet_lib32.dll, in a 32-bit Python interpreter to host the library. The corresponding DotNet64 class is created in a 64-bit Python interpreter and the DotNet64 class sends requests to the DotNet32 class which calls the 32-bit library to execute the request and then returns the response from the library.

DotNet32 ¤

DotNet32(host, port)

Bases: Server32

Wrapper around a 32-bit .NET library.

Python.NET can handle many native Python data types as input arguments.

Parameters:

Name Type Description Default
host str

The IP address (or hostname) to use for the server.

required
port int

The port to open for the server.

required
Source code in src/msl/examples/loadlib/dotnet32.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def __init__(self, host: str, port: int) -> None:
    """Wrapper around a 32-bit .NET library.

    [Python.NET](https://pythonnet.github.io/){:target="_blank"}
    can handle many native Python data types as input arguments.

    Args:
        host: The IP address (or hostname) to use for the server.
        port: The port to open for the server.
    """
    path = Path(__file__).parent / "dotnet_lib32.dll"
    super().__init__(path, "net", host, port)

    self.BasicMath = self.lib.DotNetMSL.BasicMath()
    self.ArrayManipulation = self.lib.DotNetMSL.ArrayManipulation()

add_integers ¤

add_integers(a, b)

Add two integers.

The corresponding C# code is

public int add_integers(int a, int b)
{
    return a + b;
}

See the corresponding DotNet64.add_integers method.

Parameters:

Name Type Description Default
a int

First integer.

required
b int

Second integer.

required

Returns:

Type Description
int

The sum, a + b.

Source code in src/msl/examples/loadlib/dotnet32.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def add_integers(self, a: int, b: int) -> int:
    """Add two integers.

    The corresponding C# code is

    ```csharp
    public int add_integers(int a, int b)
    {
        return a + b;
    }
    ```

    See the corresponding [DotNet64.add_integers][msl.examples.loadlib.dotnet64.DotNet64.add_integers] method.

    Args:
        a: First integer.
        b: Second integer.

    Returns:
        The sum, `a + b`.
    """
    result: int = self.BasicMath.add_integers(a, b)
    return result

add_multiple ¤

add_multiple(a, b, c, d, e)

Add multiple integers.

Calls a static method in a static class.

The corresponding C# code is

public static int add_multiple(int a, int b, int c, int d, int e)
{
    return a + b + c + d + e;
}

See the corresponding DotNet64.add_multiple method.

Parameters:

Name Type Description Default
a int

First integer.

required
b int

Second integer.

required
c int

Third integer.

required
d int

Fourth integer.

required
e int

Fifth integer.

required

Returns:

Type Description
int

The sum of the input arguments.

Source code in src/msl/examples/loadlib/dotnet32.py
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
def add_multiple(self, a: int, b: int, c: int, d: int, e: int) -> int:
    """Add multiple integers.

    Calls a static method in a static class.

    The corresponding C# code is

    ```csharp
    public static int add_multiple(int a, int b, int c, int d, int e)
    {
        return a + b + c + d + e;
    }
    ```

    See the corresponding [DotNet64.add_multiple][msl.examples.loadlib.dotnet64.DotNet64.add_multiple] method.

    Args:
        a: First integer.
        b: Second integer.
        c: Third integer.
        d: Fourth integer.
        e: Fifth integer.

    Returns:
        The sum of the input arguments.
    """
    result: int = self.lib.StaticClass.add_multiple(a, b, c, d, e)
    return result

add_or_subtract ¤

add_or_subtract(a, b, *, do_addition)

Add or subtract two C# double-precision numbers.

The corresponding C# code is

public double add_or_subtract(double a, double b, bool do_addition)
{
    if (do_addition)
    {
        return a + b;
    }
    else
    {
        return a - b;
    }
}

See the corresponding DotNet64.add_or_subtract method.

Parameters:

Name Type Description Default
a float

First double-precision number.

required
b float

Second double-precision number.

required
do_addition bool

Whether to add or subtract the numbers.

required

Returns:

Type Description
float

a + b if do_addition is True else a - b.

Source code in src/msl/examples/loadlib/dotnet32.py
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
def add_or_subtract(self, a: float, b: float, *, do_addition: bool) -> float:
    """Add or subtract two C# double-precision numbers.

    The corresponding C# code is

    ```csharp
    public double add_or_subtract(double a, double b, bool do_addition)
    {
        if (do_addition)
        {
            return a + b;
        }
        else
        {
            return a - b;
        }
    }
    ```

    See the corresponding [DotNet64.add_or_subtract][msl.examples.loadlib.dotnet64.DotNet64.add_or_subtract] method.

    Args:
        a: First double-precision number.
        b: Second double-precision number.
        do_addition: Whether to add or subtract the numbers.

    Returns:
        `a + b` if `do_addition` is `True` else `a - b`.
    """
    result: float = self.BasicMath.add_or_subtract(a, b, do_addition=do_addition)
    return result

concatenate ¤

concatenate(a, b, c, d, e)

Concatenate strings.

Calls a static method in a static class.

The corresponding C# code is

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;
}

See the corresponding DotNet64.concatenate method.

Parameters:

Name Type Description Default
a str

First string.

required
b str

Second string.

required
c str

Third string.

required
d bool

Whether to include e in the concatenation.

required
e str

Fourth string.

required

Returns:

Type Description
str

The strings concatenated together.

Source code in src/msl/examples/loadlib/dotnet32.py
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
def concatenate(self, a: str, b: str, c: str, d: bool, e: str) -> str:  # noqa: FBT001
    """Concatenate strings.

    Calls a static method in a static class.

    The corresponding C# code is

    ```csharp
    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;
    }
    ```

    See the corresponding [DotNet64.concatenate][msl.examples.loadlib.dotnet64.DotNet64.concatenate] method.

    Args:
        a: First string.
        b: Second string.
        c: Third string.
        d: Whether to include `e` in the concatenation.
        e: Fourth string.

    Returns:
        The strings concatenated together.
    """
    result: str = self.lib.StaticClass.concatenate(a, b, c, d, e)
    return result

divide_floats ¤

divide_floats(a, b)

Divide two C# floating-point numbers.

The corresponding C# code is

public float divide_floats(float a, float b)
{
    return a / b;
}

See the corresponding DotNet64.divide_floats method.

Parameters:

Name Type Description Default
a float

The numerator.

required
b float

The denominator.

required

Returns:

Type Description
float

The quotient, a / b.

Source code in src/msl/examples/loadlib/dotnet32.py
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def divide_floats(self, a: float, b: float) -> float:
    """Divide two C# floating-point numbers.

    The corresponding C# code is

    ```csharp
    public float divide_floats(float a, float b)
    {
        return a / b;
    }
    ```

    See the corresponding [DotNet64.divide_floats][msl.examples.loadlib.dotnet64.DotNet64.divide_floats] method.

    Args:
        a: The numerator.
        b: The denominator.

    Returns:
        The quotient, `a / b`.
    """
    result: float = self.BasicMath.divide_floats(a, b)
    return result

get_class_names ¤

get_class_names()

Gets the class names in the library.

Calls GetTypes using the assembly property.

See the corresponding DotNet64.get_class_names method.

Returns:

Type Description
list[str]

The names of the classes that are available in dotnet_lib32.dll.

Source code in src/msl/examples/loadlib/dotnet32.py
41
42
43
44
45
46
47
48
49
50
51
52
def get_class_names(self) -> list[str]:
    """Gets the class names in the library.

    Calls [GetTypes](https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.gettypes){:target="_blank"}
    using the [assembly][msl.loadlib.load_library.LoadLibrary.assembly] property.

    See the corresponding [DotNet64.get_class_names][msl.examples.loadlib.dotnet64.DotNet64.get_class_names] method.

    Returns:
        The names of the classes that are available in [dotnet_lib32.dll][dotnet-lib].
    """
    return ";".join(str(name) for name in self.assembly.GetTypes()).split(";")

multiply_doubles ¤

multiply_doubles(a, b)

Multiply two C# double-precision numbers.

The corresponding C# code is

public double multiply_doubles(double a, double b)
{
    return a * b;
}

See the corresponding DotNet64.multiply_doubles method.

Parameters:

Name Type Description Default
a float

First double-precision number.

required
b float

Second double-precision number.

required

Returns:

Type Description
float

The product, a * b.

Source code in src/msl/examples/loadlib/dotnet32.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def multiply_doubles(self, a: float, b: float) -> float:
    """Multiply two C# double-precision numbers.

    The corresponding C# code is

    ```csharp
    public double multiply_doubles(double a, double b)
    {
        return a * b;
    }
    ```

    See the corresponding [DotNet64.multiply_doubles][msl.examples.loadlib.dotnet64.DotNet64.multiply_doubles]
    method.

    Args:
        a: First double-precision number.
        b: Second double-precision number.

    Returns:
        The product, `a * b`.
    """
    result: float = self.BasicMath.multiply_doubles(a, b)
    return result

multiply_matrices ¤

multiply_matrices(a1, a2)

Multiply two matrices.

The corresponding C# code is

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;
    }
}

See the corresponding DotNet64.multiply_matrices method.

Parameters:

Name Type Description Default
a1 Sequence[Sequence[float]]

First matrix.

required
a2 Sequence[Sequence[float]]

Second matrix.

required

Returns:

Type Description
list[list[float]]

The result, a1 @ a2.

Source code in src/msl/examples/loadlib/dotnet32.py
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
def multiply_matrices(self, a1: Sequence[Sequence[float]], a2: Sequence[Sequence[float]]) -> list[list[float]]:
    """Multiply two matrices.

    The corresponding C# code is

    ```csharp
    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;
        }
    }
    ```

    See the corresponding [DotNet64.multiply_matrices][msl.examples.loadlib.dotnet64.DotNet64.multiply_matrices]
    method.

    Args:
        a1: First matrix.
        a2: Second matrix.

    Returns:
        The result, `a1 @ a2`.
    """
    n_rows1 = len(a1)
    n_cols1 = len(a1[0])

    n_rows2 = len(a2)
    n_cols2 = len(a2[0])

    if n_cols1 != n_rows2:
        msg = f"Cannot multiply a {n_rows1}x{n_cols1} matrix with a {n_rows2}x{n_cols2} matrix"
        raise ValueError(msg)

    m1 = self.lib.System.Array.CreateInstance(self.lib.System.Double, n_rows1, n_cols1)
    for r in range(n_rows1):
        for c in range(n_cols1):
            m1[r, c] = a1[r][c]

    m2 = self.lib.System.Array.CreateInstance(self.lib.System.Double, n_rows2, n_cols2)
    for r in range(n_rows2):
        for c in range(n_cols2):
            m2[r, c] = a2[r][c]

    ret = self.ArrayManipulation.multiply_matrices(m1, m2)
    return [[ret[r, c] for c in range(n_cols2)] for r in range(n_rows1)]

reverse_string ¤

reverse_string(original)

Reverse a string.

The corresponding C# code is

public string reverse_string(string original)
{
    char[] charArray = original.ToCharArray();
    Array.Reverse(charArray);
    return new string(charArray);
}

See the corresponding DotNet64.reverse_string method.

Parameters:

Name Type Description Default
original str

The original string.

required

Returns:

Type Description
str

The string reversed.

Source code in src/msl/examples/loadlib/dotnet32.py
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
def reverse_string(self, original: str) -> str:
    """Reverse a string.

    The corresponding C# code is

    ```csharp
    public string reverse_string(string original)
    {
        char[] charArray = original.ToCharArray();
        Array.Reverse(charArray);
        return new string(charArray);
    }
    ```

    See the corresponding [DotNet64.reverse_string][msl.examples.loadlib.dotnet64.DotNet64.reverse_string] method.

    Args:
        original: The original string.

    Returns:
        The string reversed.
    """
    result: str = self.lib.StringManipulation().reverse_string(original)
    return result

scalar_multiply ¤

scalar_multiply(a, xin)

Multiply each element in an array by a number.

The corresponding C# code is

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;
}

See the corresponding DotNet64.scalar_multiply method.

Parameters:

Name Type Description Default
a float

Scalar value.

required
xin Sequence[float]

Array to modify.

required

Returns:

Type Description
list[float]

A new array with each element in xin multiplied by a.

Source code in src/msl/examples/loadlib/dotnet32.py
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
def scalar_multiply(self, a: float, xin: Sequence[float]) -> list[float]:
    """Multiply each element in an array by a number.

    The corresponding C# code is

    ```csharp
    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;
    }
    ```

    See the corresponding [DotNet64.scalar_multiply][msl.examples.loadlib.dotnet64.DotNet64.scalar_multiply] method.

    Args:
        a: Scalar value.
        xin: Array to modify.

    Returns:
        A new array with each element in `xin` multiplied by `a`.
    """
    ret = self.ArrayManipulation.scalar_multiply(a, xin)
    return list(ret)