2

I have a python script that needs to do some communication with a C# function. The function expects three parameters, two of which are returned via the out keyword. The declaration looks something like this:

RunComputation(InfoParams locInfo, out double[,] dataTable, out int numPoints){ ... }

Since Python is a dynamically typed language, it doesn't know how to deal with the out keywords. I did some searching and found some stuff about clr.StrongBox and clr.Reference, which seem fairly simple to use in situations where you need to pass a strongly typed object.

For the numPoints parameter, I can define something like this in the python code:

 num_points = clr.Reference[int]

And it seems to be fine. The problem comes from the dataTable array. I don't know the syntax to tell it that my strongly typed object is an array of doubles. The error it gives informs me that it's expecting an instance of Type StrongBox[Array[float]]. I tried import Array from System to see if I could add that C# type in as a reference, but it failed. Any ideas?

4

1 回答 1

1

如果您只使用out参数,您也可以使用隐式方式,即如果您不out向方法传递任何参数,它们将作为元组返回。

例如

d = Dictionary[str, float]()
(keyfound,value) = d.TryGetValue("b")

它也适用于ref参数。
想象一下有以下方法:

int Foo(int x, double[,] v)

您可以通过这种方式调用该方法:

nrows = 5
ncols = 5
array = Array.CreateInstance(float, nrows, ncols)
(x, arrayNew) = obj.Foo(3, array)

最后,修改后的值array包含在arrayNew

于 2012-05-10T20:36:06.533 回答