从Python调用.NET库函数

A S*_*ena 5 .net python vb.net python-3.x python.net

我在这里已经快要精疲力尽了,希望大家能帮助我弄清楚这一点。我正在运行Anaconda Python 3.5 64位并编译了Python.NET anaconda软件包,以将.NET功能添加到Python。我导入了某人发送给我的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
)
'''
Run Code Online (Sandbox Code Playgroud)

运行初始化函数时,出现以下异常:

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)
Run Code Online (Sandbox Code Playgroud)

我对.NET相当陌生,并尝试使用Google搜索并查看与Python.NET相关的所有堆栈溢出帖子,但找不到所需的信息。另一个奇怪的事情是,当在VB中使用时,同一个DLL可以工作,而在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
Run Code Online (Sandbox Code Playgroud)

然后按照建议,我打印了过载,这是输出

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)
Run Code Online (Sandbox Code Playgroud)

我可以清楚地看到为什么Overload会抱怨,但是我针对的VB函数Initialize已将最后三个ResultEnum参数声明为可选参数,但Python明确要求。

我应该寻找一个特定的方向吗?