简体   繁体   English

如何在 python.net 中访问 System.Double[,] 对象

[英]How to access an System.Double[,] object in python.net

I'm running into a python.net problem where I cannot access ac# object in python.我遇到了一个 python.net 问题,我无法在 python 中访问 ac# 对象。 My code (writen in python) imports a DLL via the python.net CLR.我的代码(用 python 编写)通过 python.net CLR 导入了一个 DLL。 The python method runs the imported DLL method and processes the return value. python方法运行导入的DLL方法并处理返回值。 The return value is then transformed to standard python types and returned to the caller.然后将返回值转换为标准 Python 类型并返回给调用者。

The expected return object structure should look like this, a list of 4 lists with 4 doubles in each list:预期的返回对象结构应如下所示,一个包含 4 个列表的列表,每个列表中有 4 个双精度:

[ 
  [0.0, 0.0, 0.0, 0.0], 
  [0.0, 0.0, 0.0, 0.0], 
  [0.0, 0.0, 0.0, 0.0], 
  [0.0, 0.0, 0.0, 0.0],
]

In my code I parse an empty list to System.Runtime.InteropServices.VariantWrapper(), and present that to the DLL / C# method to initialise the return object.在我的代码中,我将一个空列表解析为 System.Runtime.InteropServices.VariantWrapper(),并将其呈现给 DLL/C# 方法以初始化返回对象。 The return object yields the following:返回对象产生以下结果:

result: (True, <System.Double[,] object at 0x00000201CFF944C8>)

The first argument is an acknowledgement of correct execution, the second argument is the list, at least that is what I expect.第一个参数是对正确执行的确认,第二个参数是列表,至少这是我所期望的。 When printing out the second argument and getting it's length it shows:当打印出第二个参数并获取它的长度时,它显示:

data: System.Double[,]
length: 4

Which at least suggest that the list holds the expected 4 elements, which infact should be lists again.这至少表明列表包含预期的 4 个元素,实际上应该再次列出。

The problem is that an error is thrown when I tried to loop over the inner 4 elements of the list:问题是当我尝试遍历列表的内部 4 个元素时抛出错误:

TypeError: invalid index value

I tried several different initialising types and for loops over the result, but without any succes.我尝试了几种不同的初始化类型和结果的 for 循环,但没有任何成功。

Can somebody explain what a "System.Double[,]" object should look like or how I can access it.有人可以解释“System.Double[,]”对象应该是什么样子或我如何访问它。

Below the code:代码下方:

doubleList = System.Runtime.InteropServices.VariantWrapper([ [], ])
result = dll.method("command", doubleList)
log.debug(f"result: {result}")
log.debug(f"data: {result[1]}")
log.debug(f"length: {result[1].GetLength(0)}")
for i in range(result[1].GetLength(0)):
    log.debug(f"sub: {result[1][i]}")

.NET arrays can be mutlidimensional ( int[,] ) or jagged (eg array of arrays int[][] ). .NET 数组可以是多维的( int[,] )或锯齿状的(例如数组数组int[][] )。 You are getting multidimensional array, and it is indexed like numpy multidimensional arrays, with comma between dimensions.您正在获取多维数组,它的索引类似于numpy多维数组,维度之间有逗号。

TL;DR; TL; 博士; result[1][dim0, dim1]

I just wanted to share my final solution to the problem for anyone to come across this post.我只是想分享我对这个问题的最终解决方案,让任何人都能看到这篇文章。

Although my own comment to LOST's answer is working, I came into a problem when the python list needed to be shared back to the C# DLL.尽管我自己对 LOST 答案的评论有效,但当需要将 python 列表共享回 C# DLL 时,我遇到了问题。 The native python list wasn't readable for the DLL. DLL 无法读取本机 python 列表。

I managed to use the C# methods GetValue() and SetValue() on the incoming and outgoing System.Double[,] array.我设法在传入和传出 System.Double[,] 数组上使用 C# 方法 GetValue() 和 SetValue()。 In this particular case I'm working with a 2D array, but the code can be modified to suite other array's.在这种特殊情况下,我使用的是 2D 数组,但可以修改代码以适应其他数组。

def PythonToCSharp2Darray(input_list, array_depth=(4, 4)):
    """Convert a python N-List to a C# Array."""
    a = Array.CreateInstance(Double, array_depth[0], array_depth[1])
    for i in range(array_depth[0]):
        for j in range(array_depth[1]):
            a.SetValue(input_list[i][j], i, j)
    return a

def CSharpToPython2Darray(input_array, array_depth=(4, 4)):
    """Convert a C# Array to a N-List."""
    a = []
    a.append([])
    for i in range(array_depth[0]):
        for j in range(array_depth[1]):
            a[i].append(input_array.GetValue(i, j))
        a.append([])
    return a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM