6

I'm using IronPython in VS2012 and trying to call a .Net function which takes in a Ref parameter,

Lib.dll

public int GetValue(ref double value)
{
  ...
}

Python:

import clr
clr.AddReference('Lib.dll')
from LibDll import *

value =0.0
x = GetValue(value)

am I missing something, in C# we use ref along with the variable name, what about here in Python?

2
  • I don't think Python lets you modify immutable data types like that. Perhaps you could rewrite your GetValue function so that it returns a Tuple<int, Double> or something. Commented Nov 18, 2014 at 16:31
  • Thanks Kevin, It's a Dll & I do not have access to the source code so is that's the only way, so Python supports .NET partially? Commented Nov 18, 2014 at 16:51

1 Answer 1

8

There are two ways you can invoke methods with out or ref parameters from IronPython.

In the first case the call is handled by automatic marshalling. The return value and changed refs are wrapped in a tuple and (while having 15.1 as an example double to be passed) can be used like:

(returned, referenced) = GetValue(15.1)

The more explicit way is providing a prepared clr-reference:

refParam = clr.Reference[System.Double](15.1)
result = GetValue(refParam)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Simon, could you please clarify why its 15.1?
I just chose any double value for this example as I could not have passed 'nothing' in the first case. In the second case providing nothing (i.e. having the implicit clr default value of 0.0) would have worked.

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.