Domanda

So I've managed to import a managed .NET assembly in Python .NET, and got as far as setting up several objects and calling a few functions, but I cannot figure out how this one is supposed to work.

This is the C# code that I am trying to port:

            // read event sticky clears all initial events
            CML_EVENT_STATUS status=0;
            xAxisAmp.ReadEventSticky(ref status);
            statusTextBox.Text = Convert.ToString(status);

I can import the CML_EVENT_STATUS, which reports it is <class 'CMLCOMLib.CML_EVENT_STATUS'>, but when I try to create an instance of it, I get this error:

>>> stat = CML_EVENT_STATUS()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot instantiate enumeration

If there's another way to call the function I haven't been able to figure it out. I've tried calling xAxisAmp.ReadEventSticky() and xAxisAmp.ReadEventSticky(0), which just return TypeError: No method matches given arguments.

The closest I got was this error from xAxisAmp.ReadEventSticky(CML_EVENT_STATUS):

System.ArgumentException: Object of type 'System.RuntimeType' cannot be converted to type 'CMLCOMLib.CML_EVENT_STATUS&'.

What am I doing wrong? I can't find any documentation on declaring enum types or passing them by reference, and it's driving me crazy.

È stato utile?

Soluzione

The correct way to instantiate an enum like this in Python .NET is:

stat = CML_EVENT_STATUS.EVENT_STATUS_BRAKE

Then you can call the method like so:

stat = amp.ReadEventSticky(stat)

Note that ref and out parameters don't work the same way in Python as in C#. As described in more detail in Writing iron python method with ref or out parameter, ref and out parameters are returned from a method instead of modifying the variables that were passed in. If there are multiple return values (e.g. the method returns a value and there is a ref parameter), then a tuple will be returned with the method's return value first, followed by the ref and out parameters in order.

Assuming ReadEventSticky is void, this is pretty simple: it just returns the new CML_EVENT_STATUS value.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top