我们正在建立从 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
非常感谢!