简体   繁体   English

如何在Python中将所有方法调用委托给C#DLL

[英]How to delegate all method calls to C# DLL in Python

I want to delegate all method call to C# DLL that we have written. 我想将所有方法调用委托给我们编写的C#DLL。 I am using pythonnet to load the DLL file and call the methods from the DLL. 我正在使用pythonnet加载DLL文件并从DLL调用方法。

This is my python class and it works fine, 这是我的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)

But I am not doing anything in python code except making a call to dll methods, so I don't want to write wrapper methods for all the methods in dll. 但是除了调用dll方法外,我没有在python代码中做任何事情,所以我不想为dll中的所有方法编写包装方法。

The above approach allows me to call python methods like, MyProxy().method1(first_arg, arg2, arg3, arg4) , which in turns passes the first_arg as the first argument and arg2, arg3, arg4 as an array in the second argument to self.lib.method1(first_arg, args) . 上面的方法允许我调用python方法,例如MyProxy().method1(first_arg, arg2, arg3, arg4) ,该方法first_argfirst_arg作为第一个参数,并将arg2, arg3, arg4作为数组作为第二个参数传递给self.lib.method1(first_arg, args)

This behavior is necessary for me because all of my C# methods have signature method1(String first_arg, String[] other_args) 对于我来说,此行为是必需的,因为我所有的C#方法都具有签名method1(String first_arg, String[] other_args)

How can I achieve this by only implementing __getattr__ in my python class? 如何仅通过在python类中实现__getattr__来实现此目的?

I have tried the below approach, but it throws error "No matching methods found", 我尝试了以下方法,但是会引发错误“找不到匹配的方法”,

class MyProxy:
    def __init__(self):
        self.lib = MyLibrary()

    def __getattr__(self, item):
        return getattr(self.lib, item)

Edit: I think, when I wrap a DLL method like this, 编辑:我想,当我包装这样的DLL方法时,

def method1(self, first_arg, *args):
    self.lib.method1(first_arg, args)

python takes care of converting other arguments except the first one to an array and pass that array to DLL method. python负责将除第一个参数以外的其他参数转换为数组,并将该数组传递给DLL方法。 It matches the signature of DLL method( method1(String first_arg, String[] other_args) ), since python passes the second argument as an array. 它与DLL方法( method1(String first_arg, String[] other_args) )的签名匹配,因为python将第二个参数作为数组传递。

Can we do anything in __getattr__ method to do the array conversion of other arguments except first one and pass on to DLL methods ? 我们可以在__getattr__方法中做任何事情来进行除第一个参数以外的其他参数的数组转换并传递给DLL方法吗?

Not tested, but something like this might work: 未经测试,但类似的事情可能会起作用:

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.

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