从IronPython调用vb.net方法

时间:2015-09-22 09:01:13

标签: vb.net ironpython

我想创建一个充满Vb.net的命名空间可以从Python访问的方法。这是我正在尝试的内容:

VB.net PyMethods:

Namespace PyApp

    Public Class PyMethods

        Public Sub MessageBoxPop(ByVal e As Object, ByVal StringSent As String)
            MsgBox(StringSent)
        End Sub

    End Class

End Namespace

函数调用Python脚本

Friend Function ExecutePyCreator(ByVal Filename As String) As CreatePyObject

    Dim ReturnedPyObject As New CreatePyObject

    Dim pyEngine As ScriptEngine = Nothing
    Dim pyScope As ScriptScope = Nothing
    Dim pySource As ScriptSource = Nothing

    Dim Location As String = System.Environment.GetCommandLineArgs()(0)
    Dim Assembly As String = System.IO.Path.GetFileName(location).Replace(".exe", "")

    Try
        pyEngine = Python.CreateEngine()
        pyScope = pyEngine.CreateScope()
        pyScope.SetVariable("WebSailor", Me.Handle)

        pySource = pyEngine.CreateScriptSourceFromFile(Filename)

    Catch ex As Exception
        _wcm(ex.Message.ToString, "Error", "ExecutePyWebBrowser( " & Filename.ToString & " )")
    End Try

    Try

        pyScope.ImportModule("clr")

        pyEngine.Execute("import clr", pyScope)

        pyEngine.Execute("clr.AddReference(""" & Assembly & """)", pyScope)

        '    MsgBox("from " & appName & ".PyApp import PyMethods")

        pyEngine.Execute("from " & Assembly & ".PyApp import PyMethods", pyScope)

    Catch ex As Exception
        _wcm(ex.Message.ToString, "Error", "ExecutePyWebBrowser( pyScope.ImportModule(pyScope) )")
        Return Nothing

    End Try

    Try
        pySource.Execute(pyScope)

    Catch ex As Exception
        _wcm(ex.Message.ToString, "Error", "ExecutePyWebBrowser( pySource.Execute(pyScope) )")
        Return Nothing
    End Try

    Try
        Dim t As Object = pyScope.GetVariable("PyCreateObjectEx")
        Dim o As Object = pyEngine.Operations.CreateInstance(t)
        '   Dim r As Object = pyEngine.Operations.InvokeMember(o, "Go")


        With ReturnedPyObject
            .ObjectType = CStr(pyEngine.Operations.InvokeMember(o, "GetObjectType"))
            .HomeTitle = CStr(pyEngine.Operations.InvokeMember(o, "GetTitle"))
            .HomeURL = CStr(pyEngine.Operations.InvokeMember(o, "GetURL"))
        End With

        Return ReturnedPyObject

        '  pyEngine.Operations.Engine.

    Catch ex As Exception
        _wcm(ex.Message.ToString, "Error", "ExecutePyWebBrowser( pySource.Execute(pyScope) )")

    End Try

    Return Nothing

End Function

Python脚本

class PyCreateObjectEx(object):
  def __init__(self):
    self.ObjectType = "WebBrowserEx"
    self.HomeTitle = "Google"
    self.HomeURL = "http://google.com"
  def MessageBox(self, e):
    PyMethods.MessageBoxPop(e)
  def GetTitle(self):
    return self.HomeTitle
  def GetURL(self):
    return self.HomeURL
  def GetObjectType(self):
    PyCreateObjectEx.MessageBox(self, "Test")
    return self.ObjectType

我已经尝试过使用@classmethod和@staticmethod,但我不知道如何正确使用它。任何帮助将不胜感激。

0 个答案:

没有答案