我差不多在这里结束了,我希望你们都能帮助我解决这个问题。我正在运行Anaconda Python 3.5 64位并编译Python.NET anaconda包以向Python添加.NET功能。我导入了一个由我发送给我的DLL,这是我的代码的样子:
from __future__ import (
unicode_literals,
print_function,
division,
absolute_import
)
import clr
from System import String, Char, Int32
clr.setPreload(True)
clr.AddReference('System.Windows.Forms')
clr.AddReference(
"C:\\Program Files\\XYZ\\TTE.dll"
)
import TTE
from System.Windows.Forms import Form, Application, Button
import System
tt = TTE.TT()
form = Form()
# declaring string (not python native string) to get System.String
cdbp = String('C:\\')
sdbp = String('C:\\')
mdbp = String('C:\\')
tt.Initialize(cdbp, sdbp, mdbp, form)
'''
tt.Initialize.Overloads[
System.String, System.String, System.String, System.Windows.Forms.Form](
cdbp, sdbp, mdbp, form
)
'''
当我运行Initialize函数时,我得到以下异常:
Traceback (most recent call last):
File "C:/Users/as/All/Code/try1.py", line 257, in <module>
tt.Initialize(cdbp, sdbp, mdbp, form)
System.ArgumentException: Object of type 'System.Int32' cannot be converted to type 'TTE.TT+ResultEnum&'.
at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Python.Runtime.MethodBinder.Invoke(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, MethodInfo[] methodinfo)
我是.NET的新手,并尝试使用Google搜索并查看与Python.NET相关的所有堆栈溢出帖子,但我找不到我要查找的信息。另一个奇怪的事情是,相同的DLL在VB中使用时工作,在python中使用时失败。为什么会这样?
我认为问题在于Python.NET认为我正在调用一个名为Initialize的不同(重载)函数,所以我尝试了Overloads调用(在注释中)并且它给了我以下异常:
Traceback (most recent call last):
File "C:/Users/as/All/Code/try1.py", line 261, in <module>
System.Windows.Forms.Form](cdbp, sdbp,
TypeError: No match found for signature
然后根据建议,我打印了Overload,这是输出
Int32 Initialize(System.String ByRef, System.String ByRef, System.String ByRef, System.Windows.Forms.Form, ResultEnum ByRef, ResultEnum ByRef, ResultEnum ByRef)
Int32 Initialize(System.Collections.Specialized.StringCollection ByRef, System.String ByRef, System.String ByRef, System.Windows.Forms.Form, ResultEnum ByRef, ResultEnum ByRef, ResultEnum ByRef)
Int32 Initialize(System.Collections.Generic.List`1[System.String] ByRef, System.String ByRef, System.String ByRef, System.Windows.Forms.Form, ResultEnum ByRef, ResultEnum ByRef, ResultEnum ByRef)
我可以清楚地看到为什么Overload正在抱怨,但我正在定位的VB函数Initialize已经将最后三个ResultEnum参数声明为可选,但Python显然需要它。
我应该注意一个特定的方向吗?