IronPython,将引用传递给Byte

时间:2014-12-24 16:34:59

标签: c# ironpython

我有一个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]

我一直试图解决这些问题好几天但却无法解决问题。 谢谢!

1 个答案:

答案 0 :(得分:1)

我认为你的调用的第二个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;
        }
    }
}

IronPython的:

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