Explicit Type Casting in Python.NET

96 Views Asked by At

I am working with a .NET Framework DLL from Python using Python.NET.

The DLL allows you to fetch the parent of a given object, but will return the parent as an instance of the interface IEngineeringObject rather than its actual type DeviceItem - and I've been unable to find out how to cast the returned object to the correct type from Python.

In C# I can explicitly cast the type of the returned object to DeviceItem:

DeviceItem dev_item = (DeviceItem)address.Parent;

I've tried manually assigning the class (where _hw is the imported DLL namespace):

parent.__class__ = _hw.DeviceItem

but get errors saying:

TypeError: __class__ assignment: 'DeviceItem' object layout differs from 'IEngineeringObject'

Am I barking up completely the wrong tree here? Similar threads have discussed writing a helper function in C# and creating a custom DLL to be used from Python, but if there is any hope of doing it natively in Python, that would be ideal.

0

There are 0 best solutions below