[英]passing a delegate function back to a clr dll which calls a mfc c++ dll in c#
[英]How to delegate all method calls to C# DLL in Python
我想将所有方法调用委托给我们编写的C#DLL。 我正在使用pythonnet加载DLL文件并从DLL调用方法。
这是我的python类,工作正常,
import clr
clr.AddReference('MyDll')
from MyDll import MyLibrary
class MyProxy:
def __init__(self):
self.lib = MyLibrary()
def method1(self, first_arg, *args):
self.lib.method1(first_arg, args)
def method2(self, first_arg, *args):
self.lib.method2(first_arg, args)
但是除了调用dll方法外,我没有在python代码中做任何事情,所以我不想为dll中的所有方法编写包装方法。
上面的方法允许我调用python方法,例如MyProxy().method1(first_arg, arg2, arg3, arg4)
,该方法first_arg
将first_arg
作为第一个参数,并将arg2, arg3, arg4
作为数组作为第二个参数传递给self.lib.method1(first_arg, args)
。
对于我来说,此行为是必需的,因为我所有的C#方法都具有签名method1(String first_arg, String[] other_args)
如何仅通过在python类中实现__getattr__
来实现此目的?
我尝试了以下方法,但是会引发错误“找不到匹配的方法”,
class MyProxy:
def __init__(self):
self.lib = MyLibrary()
def __getattr__(self, item):
return getattr(self.lib, item)
编辑:我想,当我包装这样的DLL方法时,
def method1(self, first_arg, *args):
self.lib.method1(first_arg, args)
python负责将除第一个参数以外的其他参数转换为数组,并将该数组传递给DLL方法。 它与DLL方法( method1(String first_arg, String[] other_args)
)的签名匹配,因为python将第二个参数作为数组传递。
我们可以在__getattr__
方法中做任何事情来进行除第一个参数以外的其他参数的数组转换并传递给DLL方法吗?
未经测试,但类似的事情可能会起作用:
class MyProxy:
def __init__(self):
self.lib = MyLibrary()
def __getattr__(self, item):
lib_method = getattr(self.lib, item)
def _wrapper(first_arg, *args):
return lib_method(first_arg, args)
return _wrapper
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.