views:

621

answers:

2

I am trying to replicate this example of calling IronPython code from VB.net. I've downloaded and installed IronPython and created a VB.net console application. I added references to all of the dlls in the IronPython installation folder:

  • IronPython
  • IronPython.Modules
  • IronPythonTest
  • Microsoft.Scripting.ExtensionAttribute
  • Microsoft.Scripting
  • Microsoft.Scripting.Core

And I have this source code:

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

I receive the error "Name 'PythonEngine' is not declared."

I can't seem to find PythonEngine when I search the ObjectBrowser.

Is the example out of date or do I have an error?

A: 

Things seem to have changed.

I have not worked on IronPython & have downloaded it now.
I tried relating your code with what is inside IronPython.DLL

  • Add reference to IronPython.dll
  • Add statement for imports - "Imports IronPython.Hosting".

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

I guess rest of your code should work as it is.
Load the Ironpython.dll into reflector & you will be able to see the types/methods/properties.

Hope that helps.

shahkalpesh
A: 

I found this link in the FAQ that came with IronPython 2.0.1 have you read through it before? Aaron Marten : A bit more on IronPython

Charles Y.