pythonnet无法从vb .net dll调用方法 - TypeError

时间:2014-03-14 14:04:07

标签: python .net vb.net dll python.net

我们正在建立从python到.net VB代码的连接。我们在VB中成功创建了DLL,我们可以使用CLR将其导入python。导入DLL中的类,并且所有方法都可见。但是当我们调用一个方法时,我们会失败并使用TypeErrors - 即使没有参数的方法也会失败。 PS。其他(标准)VB方法工作正常(即from System import String a=String("Some string")) PS2。用C#编写的代码也适用于这种方法

与.dll连接的Python代码:

#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-    
    import os,sys
    import site
    scriptPath=r"E:\Dropbox\SISTeMA\TRE development\TRE Add-In\visum\Visum_VB_interactions\From PTV\VisumNetAddIn\32Bit"
    assemblyPath=r"E:\Dropbox\SISTeMA\TRE development\TRE Add-In\visum\Visum_VB_interactions\Interact\bin\Debug\Interact.dll"
    site.addsitedir(scriptPath)
    import clr
    import System
    assemblyPath = os.path.join(scriptPath,assemblyPath)
    site.addsitedir(os.path.dirname(assemblyPath))
    assemblyFile,extension  = os.path.splitext(os.path.basename(assemblyPath))
    clr.AddReference(assemblyFile)   
    from Interact import Interact_Class

VB .net类编译为dll:

Imports System
Public Class Interact_Class
    Public s As String

    Public Sub give_me()
        s = "got it"
    End Sub

    Public Sub give_me_sth(ByVal sth As String)
        s = sth
    End Sub

    Public Function I_will_give_you() As String
        Return "Here You go"
    End Function
End Class

Python调用:

from Interact import Interact_Class
print Interact_Class.give_me
Interact_Class.give_me()
from System import String
s=String("I will give You")
print Interact_Class.give_me_sth(s)
print Interact_Class.I_will_give_you()

产生的错误:

Interact_Class.give_me()
TypeError: not enough arguments

print Interact_Class.give_me_sth(s)
TypeError: No method matches given arguments

print Interact_Class.I_will_give_you()
TypeError: not enough arguments

非常感谢!

1 个答案:

答案 0 :(得分:3)

在Python代码中,您使用的是VB类,就好像它是C#术语中的静态类,或者是VB.Net术语中的模块。

你需要在python中创建它的一个实例,然后调用实例上的方法,而不是类定义。