[英]How do I pass a ulong type argument to a C# function from Python?
From Python, I am calling a C# function which is in a dll.从 Python 中,我正在调用一个位于 dll 中的 C# 函数。 This is done by "python.net".这是由“python.net”完成的。 Most of the functions can be called correctly in Python.大多数函数可以在 Python 中正确调用。 However, there is one function I have a problem because of data type on the argument.但是,由于参数上的数据类型,我遇到了一个函数问题。
In C#, there is a function, that takes an unsigned long type:在 C# 中,有一个函数,它采用 unsigned long 类型:
bool SetValueAsInt64U(ulong rValue);
In Python, I am calling it with在 Python 中,我用
SetValueAsInt64U(22);
This gives the error message: TypeError: No method matches given arguments for SetValueAsInt64U:(<class 'int'>)这给出了错误消息:TypeError:没有方法匹配 SetValueAsInt64U 的给定参数:(<class 'int'>)
I also tried to use the ctypes, and call the function like:我还尝试使用 ctypes,并调用如下函数:
SetValueAsInt64U(c_ulong(22));
This gives a similar error: TypeError: No method matches given arguments for SetValueAsInt64U: (<class 'ctypes.c_ulong'>)这给出了一个类似的错误:TypeError: No method matching given arguments for SetValueAsInt64U: (<class 'ctypes.c_ulong'>)
For more information, I am showing a correct call.有关更多信息,我正在显示正确的调用。 In C#, there is another function with signed argument:在 C# 中,还有另一个带符号参数的函数:
bool SetValueAsInt64(long rValue)
In Python, I can call the function without any error message, for example:在 Python 中,我可以在没有任何错误消息的情况下调用该函数,例如:
SetValueAsInt64(-5);
So the question is how can I call the C# function with the unsigned type of argument from Python?所以问题是如何使用 Python 中的无符号类型参数调用 C# 函数?
在这种情况下,我要做的就是重载该方法以接受 int 参数,如果达到最大 int 范围 2,147,483,647,python 会将其解析为 ulong。
Use numpy.使用麻木。
import numpy as np
dp.SetValueAsInt64U(np.uint64(22));
This approach does not need to change C# code.这种方法不需要更改 C# 代码。 It is a solution in Python code.它是 Python 代码中的解决方案。
The numpy data types are: https://numpy.org/devdocs/user/basics.types.html numpy 数据类型是: https : //numpy.org/devdocs/user/basics.types.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.