I'm trying to use IronPython as a scripting language in a C# application. The scripts will have to use functions furnished by the main application (implemented in the application or in external libraries).
While this works pretty perfectly for "simple" functions with only input parameters (like in any other examples I found), this fails with functions with out parameters.
Example (C# code) :
public delegate int getValue_delegate(out float value);
public int getValue(out float value)
{
value = 3.14F;
return 42;
}
public void run(string script, string func)
{
ScriptRuntime runtime = ScriptRuntime.CreateFromConfiguration();
ScriptEngine engine = runtime.GetEngine("Python");
ScriptScope scope = engine.CreateScope();
scope.SetVariable("myGetTemp", new getValue_delegate(getValue));
engine.ExecuteFile(script, scope);
}
Then the IronPython script. I expect that value should be set to 3.14, but only manage to get 0.0.
import clr
import System
ret,value = getValue()
print("getValue -> %d => %s" % (ret, value)) # => output "getValue -> 42 => 0.0
value = clr.Reference[System.Single]()
ret = getValue(value)
print("getValue -> %d => %s" % (ret, value)) # => output "getValue -> 42 => 0.0
Am I missing something?
Notes:
- Out parameters works also perfectly with functions from the standard library.
- Most of the times, as I'm using functions from external libraries, it is not possible to change the method signature to avoid using out parameters.