开发者

Passing an array from IronPython to C# library

I am trying to understand how to pass a multi-dimensional array of float from IronPython code to a C# library.

Here is the C# code I am trying to call (This is a function is a library class I am importing into my IronPython code):

public void ShowMessage(double[,] values)

This is the my IronPython code:

开发者_Go百科
import clr
clr.AddReferenceToFile(r"DisplayLib.dll")
from DisplayLib import Display

display = Display()

a = [[1.2, 1.3, 1.4, 1.5],
     [2.2, 2.3, 2.4, 2.5]]

display.ShowMessage(a)

I am getting the following exception: "expected Array[float], got list" then I tried to convert the array to a tuple but it only worked for a 1D array.

Any suggestions on how do this?


You'll need to create an instance of a Two-Dimensional .NET array. You cannot use Python lists in place of arrays. An unfortunate limitation.

You could try something like this:

from System import Array

data = [[1.2, 1.3, 1.4, 1.5],
        [2.2, 2.3, 2.4, 2.5]]
# assuming all rows will have the same length
a = Array.CreateInstance(float, len(data), len(data[0]))
for i, row in enumerate(data):
    for j, col in enumerate(row):
        a[i, j] = col
display.ShowMessage(a);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜