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

3 投票
1 回答
2856 浏览
提问于 2025-04-17 22:19

我们正在尝试从Python连接到VB的.NET代码。我们已经成功在VB中创建了一个DLL,并且可以通过CLR将它导入到Python中。DLL中的类已经导入,所有的方法都能看到。但是,当我们调用某个方法时,却出现了类型错误,即使是没有参数的方法也会失败。

另外,其他标准的VB方法运行得很好,比如:from System import String a=String("Some string")

而用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

这是编译成dll的VB .net类:

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 个回答

3

在你的Python代码中,你把VB类当成了C#中的静态类,或者VB.Net中的模块来使用。

你需要在Python中先创建这个类的一个实例,然后再通过这个实例来调用方法,而不是直接用类的定义来调用。

撰写回答