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?

2
  • I don't know much about IronPython programming, but perhaps you can consider adding a C# wrapper function that takes your single InfoParams parameter and returns an object composing of both the double[,] dataTable and int numPoints. That way C# handles the outs and IronPython just gets an object from which it can access that data via its two exposed properties. Commented May 10, 2012 at 20:22
  • Makes sense. I'll give that a go while I wait to see if someone has a direct solution. Commented May 10, 2012 at 20:24

1 Answer 1

1

If you are using only out arguments you can also use the implicit way, i.e. if you don't pass any out argument to the method, they're returned as a tuple.

e.g.

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

It works also with ref arguments.
Imagine to have the following method:

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

you can call the method in this way:

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

and finally, the modified value of array is contained into arrayNew

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.