代码之家  ›  专栏  ›  技术社区  ›  pc1oad1etter

试图从中调用IronPythonVB.net版-“未声明名称‘PythonEngine’。”

  •  0
  • pc1oad1etter  · 技术社区  · 15 年前

    我在尝试复制 this example of calling IronPython IronPython 创造了一个VB.net版控制台应用程序。我添加了对IronPython安装文件夹中所有DLL的引用:

    • 铁蟒
    • IronPython.模块
    • 铁蟒蛇
    • Microsoft.Scripting.ExtensionAttribute文件
    • Microsoft.Scripting.Core

    我有这个源代码:

    Option Explicit On
    Option Strict On
    
    Imports Microsoft.Scripting.Hosting
    Imports Microsoft.Scripting
    Imports IronPython.Hosting
    Imports IronPython.Runtime.Types
    
    Module Module1
        Public Class HelloWorldVB
            Public Overridable Function HelloWorld(ByVal name As String) As String
                Return String.Format("Hello '{0}' from Visual Basic", name)
            End Function
        End Class
    
        Sub Main()
            Dim helloWorld As New HelloWorldVB()
            Console.WriteLine(helloWorld.HelloWorld("Maurice"))
    
            Dim runtime As ScriptRuntime = PythonEngine.CurrentEngine.Runtime
            Dim scope As ScriptScope = Runtime.ExecuteFile("HelloWorld.py")
            Dim pythonType As PythonType = scope.GetVariable(Of PythonType)("HelloWorldIronPython")
            helloWorld = CType(Runtime.Operations.Call(pythonType), HelloWorldVB)
            Console.WriteLine(helloWorld.HelloWorld("Maurice"))
    
            Console.ReadLine()
        End Sub
    End Module
    

    我收到错误“名称'PythonEngine'未声明。”

    这个例子过时了还是我有错误?

    2 回复  |  直到 15 年前
        1
  •  0
  •   shahkalpesh    15 年前

    事情好像变了。

    我没有在IronPython上工作过&现在已经下载了它。
    我试着把你的代码和里面的东西联系起来IronPython.DLL

    • 添加引用到IronPython.dll
    • Add imports语句-“imports”IronPython.托管".

    Dim scriptEngine as Python.CreateEngine()
    Dim scope As ScriptScope = scriptEngine.ExecuteFile("myfile.py")

    我想你剩下的代码应该能正常工作。
    加载Ironpython.dll进入reflector&您将能够看到类型/方法/属性。

    希望有帮助。

        2
  •  0
  •   Charles Y.    15 年前

    我在ironpython2.0.1附带的FAQ中找到了这个链接,您以前读过吗? Aaron Marten : A bit more on IronPython