gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 01:19:09 25 4
gpt4 key购买 nike

我们正在建立从 python 到 .net VB 代码的连接。我们在VB中成功创建了DLL,我们可以使用CLR将它导入到python中。 DLL 中的类已导入,所有方法都可见。然而,当我们调用一个方法时,我们会因 TypeErrors 而失败——即使是没有参数的方法也会失败。附言。其他(标准)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

编译为 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

非常感谢!

最佳答案

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

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

关于pythonnet 无法从 vb .net dll 调用方法 - TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22407248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com