简体   繁体   中英

Pythonnet TypeError when initializing class

I'm trying to access a class inside a.dll file using pythonnet and I am unable to create and instance of a class without the following error. I'm using the latest version of pythonnet (2.5.2) and Python 3.10.5.

Error

Traceback (most recent call last):
    x = IDispenser()
TypeError: interface takes exactly one argument

Python Code

import clr
import sys

dll_path = "C:\\Program Files\\Seyonic\\Dispenser Control Monitor 3.8"

if dll_path not in sys.path:
    sys.path.append("C:\\Program Files\\Seyonic\\Dispenser Control Monitor 3.8")

assert dll_path in sys.path

clr.AddReference("Seyonic.Dispenser")
from Seyonic.Dispenser import IDispenser

x = IDispenser()

DLL

namespace Seyonic.Dispenser
{
public interface IDispenser
{
// ----------------------------------------------------------------------------
// Properties
// ----------------------------------------------------------------------------
int Data { get;}
...etc
}
}

After digging through the dll file (doesn't seem to take any arguments for using these classes) I think this is a pythonnet / python3 problem. Here is an excerpt from https://github.com/DynamoDS/Dynamo/wiki/Work-in-progress-to-improve-Python-3-support :

Python classes cannot implement .NET interfaces (Not planned) This is probably an advanced usage scenario of Python and interoperability with .NET. Here a Python class is defined in such a way that it implements a .NET interface. The idea behind this is that instances of the class can be created and passed as arguments to methods accepting them. Here is an example of how this could look like:

import clr
clr.AddReference('SomeLibrary')
from SomeLibrary import SomeInterface, SomeClass

class MyClass(SomeInterface):
  def __init__(self):
    pass

inst = MyClass()
result = SomeClass.SomeMethodTakingSomeInterface(inst)
OUT = result

Given a valid library was provided, the previous code sample would work without issues in the IronPython2 engine. When using CPython3, however, you can get TypeError: interface takes exactly one argument or TypeError: object does not implement SomeInterface, depending on the interface and method definitions. The required changes in Python .NET to make this work seem big when compared to the relevance we have detected for this use case, so we have decided to not plan this work yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM