PythonNET: C# function does not modify class instanced passed to it

415 Views Asked by At

I have a C# class like

public MY_CLASS(int number)
        {
            SomeField = number;
            SetElseWhere = 0;
        }
    }

that I want to pass to a C# function like

public static bool MyTask(string pathXML, out MY_CLASS test)

I expect MyTask to modify the field SetElseWhere in an instance of MY_CLASS

In PythonNet I call the function like

import System

my_dll = System.Reflection.Assembly.LoadFile('example.dll')
MY_CLASS_t = my_dll.GetType('NAMESPACE.MY_CLASS')

my_instance = System.Activator.CreateInstance(MY_CLASS_t)

x = MyTask('test.xml', my_instance)

Now it is getting strange, at least to me:

The returned value x is a tuple with two entries, a boolan and an object of type NAMESPACE.MY_CLASS. But it is not the instance that I sent in as second parameter stated with property out.

When I check the results I can see that the object in the returned tuple has been modified correctly, but the instance I send in has not been modified.

Any ideas why?

1

There are 1 best solutions below

0
On BEST ANSWER

Ok, I finally understood your problem and this issue is not documented. There is no concept of out/ref arguments in Python, hence the modifications to arguments are returned in the tuple.

Here is an open issue about this:

https://github.com/pythonnet/pythonnet/issues/228