【问题标题】:Passing an array from IronPython to C# library将数组从 IronPython 传递到 C# 库
【发布时间】:2011-09-16 05:03:00
【问题描述】:

我试图了解如何将一个多维浮点数组从 IronPython 代码传递到 C# 库。

这是我试图调用的 C# 代码(这是一个函数,是我要导入到 IronPython 代码中的库类):

public void ShowMessage(double[,] values)

这是我的 IronPython 代码:

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)

我收到以下异常:“expected Array[float], got list”然后我尝试将数组转换为元组,但它仅适用于一维数组。

关于如何做到这一点的任何建议?

【问题讨论】:

    标签: c# ironpython


    【解决方案1】:

    您需要创建一个二维 .NET 数组的实例。您不能使用 Python 列表代替数组。不幸的限制。

    你可以试试这样的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多