将字节作为参数传递给c#?我在尝试从python调用c#方法时遇到问题。我正在使用python3.2而不是IronPython。我用pip安装了最新版本的python.net,但在使用ref或out参数时遇到问题(经常讨论)。到目前为止,这是我的代码:importclrpath=clr.FindAssembly("USB_Adapter_Driver")clr.AddReference(path)fromUSB_Adapter_DriverimportUSB_Adaptergpio=USB_Adapter()version2=''status,version=gpio.version(version2)print('status:'+str(status))print('Version:'+str(version))readMask=bytearray([1])writeData=bytearray([0])print(readMask)print(writeData)状态,readData=gpio.gpioReadWrite(b'x01',b'x00',b'x00')状态,readData=gpio.gpioReadWrite(readMask[0],writeData[0],b'x00')状态,readData=gpio。gpioReadWrite(readMask[0],writeData[0],)我遇到了一些重大问题。一直在运行。但是在这个确切的配置中它似乎可以工作(我需要将路径保存到变量否则它不会工作,而且我不能在clr.AddReference(path)中键入路径dll因为那不会工作)c#version方法如下所示:publicUSB_Adapter_Driver.USB_Adapter.Statusversion(refstringver)我的状态变量得到一个与c#类的状态枚举完全匹配的值。问题是:调用后我的变量“version”为空。为什么?根据:Howtouse.NETmethodsmodifiedinPython?这应该是一种合法的做事方式。我也尝试使用显式版本,但我的命名空间clr不包含clr.Reference()。下一个(也是更严重的)问题是pio.gpioReadWrite()。这里有关于此的信息:publicUSB_Adapter_Driver.USB_Adapter.StatusgpioReadWrite(bytereadMask,bytewriteData,refbytereadData)这里我得到错误:TypeError:没有方法匹配给定的参数我从上面使用哪个调用并不重要。所有这些都失败了。这是调试运行的完整输出:d:[projectpath]tests.py(6)()status:6Version:bytearray(b'x01')bytearray(b'x00')未捕获的异常。进入事后调试运行'cont'或'step'会重启程序d:[projectpath]tests.py(28)()status,readData=gpio.gpioReadWrite(readMask[0],writeData[0],)(Pdb)Traceback(最近调用最后一次):文件“D:WinPython-64bit-3.4.4.2Qt5python-3.4.4.amd64libpdb.py”,第1661行,在主pdb._runscript(mainpyfile)文件“D:WinPython-64bit”中-3.4.4.2Qt5python-3.4.4.amd64libpdb.py”,第1542行,在_runscriptself.run(statement)文件“D:WinPython-64bit-3.4.4.2Qt5python-3.4.4.amd64libbdb.py”,第431行,在runexec(cmd,globals,locals)File"",line1,inFile"d:[projectpath]tests.py",line28,instatus,readData=gpio.gpioReadWrite(readMask[0],writeData[0],)TypeError:没有方法匹配给定的参数希望你们中的一个知道如何解决这个问题。谢谢,KevinPython.Net不像IronPython那样处理ref/out参数。status,readData=gpio.gpioReadWrite(b'x01',b'x00',b'x00')调用不太正确,因为Python.Net不会将更新的readData作为第二个结果返回。您可以使用反射来处理ref参数。在这里查看我对类似问题的回答。您的案例有一个粗略的代码模板:以上是C#学习教程:将字节作为参数传递给c#?如果分享的内容对你有用,需要进一步了解C#学习教程,希望你多多关注——importclrclr.AddReference("USB_Adapter_Driver")importSystemimportUSB_Adapter_DrivermyClassType=System.Type。GetType("USB_Adapter_Driver.USB_Adapter,USB_Adapter_Driver")方法=myClassType.GetMethod("gpioReadWrite")参数=System.Array[System.Object]([System.Byte(1),System.Byte(0),System.Byte(0)])gpio=USB_Adapter_Driver.USB_Adapter()status=method.Invoke(gpio,parameters)readData=parameters[2]本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
