[IronPython] Creating Python classes in IP2

Curt Hagenlocher curt at hagenlocher.org
Sun Mar 30 13:53:05 CEST 2008

On Sun, Mar 30, 2008 at 2:59 AM, Paul Turbett <sls at iinet.net.au> wrote:
> I've looked through the DLR hosting spec, and experimented a bit, but I
> can't see how to get the same thing working with DLR hosting. So, how do
> I create instances of python classes from C#?

This is what works for me:

    ScriptRuntime runtime = ScriptRuntime.Create();
    ScriptEngine engine = runtime.GetEngine("py");

    // These next two lines are needed if you expect to be able to import System
    // without manually loading the assembly through Python
    // runtime.LoadAssembly(typeof(string).Assembly);
    // runtime.LoadAssembly(typeof(System.Diagnostics.Debug).Assembly);

    // File foo.py contains class Foo
    ScriptScope scope = runtime.ExecuteFile("foo.py");
    object theClass;
    scope.TryGetVariable("Foo", out theClass);
    object theValue = PythonCalls.Call(theClass);

Curt Hagenlocher
curt at hagenlocher.org

More information about the Ironpython-users mailing list