[英]How do I pass a ulong type argument to a C# function from Python?
从 Python 中,我正在调用一个位于 dll 中的 C# 函数。 这是由“python.net”完成的。 大多数函数可以在 Python 中正确调用。 但是,由于参数上的数据类型,我遇到了一个函数问题。
在 C# 中,有一个函数,它采用 unsigned long 类型:
bool SetValueAsInt64U(ulong rValue);
在 Python 中,我用
SetValueAsInt64U(22);
这给出了错误消息:TypeError:没有方法匹配 SetValueAsInt64U 的给定参数:(<class 'int'>)
我还尝试使用 ctypes,并调用如下函数:
SetValueAsInt64U(c_ulong(22));
这给出了一个类似的错误:TypeError: No method matching given arguments for SetValueAsInt64U: (<class 'ctypes.c_ulong'>)
有关更多信息,我正在显示正确的调用。 在 C# 中,还有另一个带符号参数的函数:
bool SetValueAsInt64(long rValue)
在 Python 中,我可以在没有任何错误消息的情况下调用该函数,例如:
SetValueAsInt64(-5);
所以问题是如何使用 Python 中的无符号类型参数调用 C# 函数?
在这种情况下,我要做的就是重载该方法以接受 int 参数,如果达到最大 int 范围 2,147,483,647,python 会将其解析为 ulong。
使用麻木。
import numpy as np
dp.SetValueAsInt64U(np.uint64(22));
这种方法不需要更改 C# 代码。 它是 Python 代码中的解决方案。
numpy 数据类型是: https : //numpy.org/devdocs/user/basics.types.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.