IronPython,传递对字节的引用

本文关键字:字节 引用 IronPython | 更新日期: 2023-09-27 18:35:23

我有一个C#函数,我试图从IronPython调用。它被定义为...

public status extern int OpenNetPort(int Port, string IPaddr, ref byte ComAddr, ref int PortHandle)

我执行以下操作...

clrType = Type.GetType('System.Byte')
d = 0
comAdr = clr.Reference[Byte](clr.Convert(d, clrType))
rfidHandle = StaticClassReaderB.OpenNetPort(27011, '192.168.0.250', comAdr, 27011)
I get the following when running...
TypeError: expected Byte, got StrongBox[Byte]

几天来我一直在尝试解决它,但无法正常工作。谢谢!

IronPython,传递对字节的引用

我认为你调用的第二个ref论点是错误的。无论出于何种原因,IronPython消息都具有误导性。

c#原型仍然有点不清楚,可能会隐藏更多的惊喜。要找出方法的明确签名,您可以使用ildasm

完整的示例,其中包含我所做的其他假设:

C#

namespace callingdotnet {
    public class Callingdotnet {
        public static int OpenNetPort(int Port, string IPaddr, ref byte ComAddr, ref int PortHandle) {
            Console.WriteLine("port: " + Port);
            Console.WriteLine("IPaddre: " + IPaddr);
            Console.WriteLine("ComAddr: " + ComAddr);
            Console.WriteLine("PortHandle: " + PortHandle);
            ComAddr = 42;
            PortHandle = 0xdead;
            return 0;
        }
    }
}

铁蟒:

import System
import clr
clr.AddReferenceToFileAndPath(r"bin'debug'callingdotnet.dll")
import callingdotnet
d = 123
comAddr = clr.Reference[System.Byte](d)
p = 423564
PortHandle = clr.Reference[System.Int32](p)
ret = callingdotnet.Callingdotnet.OpenNetPort(27011, '192.168.0.250', comAddr, PortHandle)
print "-"*40
print "ret = ", ret
print "comAddr: ", comAddr.Value
print "PortHandle: %x" % PortHandle.Value

输出:

$ ~/github/IronLanguages/bin/Debug/ipy.exe -S example-byte-ref.py
port: 27011
IPaddre: 192.168.0.250
ComAddr: 123
PortHandle: 423564
----------------------------------------
ret =  0
comAddr:  42
PortHandle: dead