[IronPython] strange runtime error hosting Iron Python in C# app

Dino Viehland dinov at exchange.microsoft.com
Tue Apr 3 17:35:50 CEST 2007


Oh, this is probably a loader context issue.  Do you need to setup sys.path in order to clr.AddRefernece your assembly?  If so then your assembly is most likely getting loaded twice.  Once in the normal load context and once in the load file context (which is the context that files that live on sys.path but outside of your app domain base will get loaded into).

Probably what's best would be for you to get the assembly object from one of your types (typeof(IFoo).GetAssembly()) and then add that reference directly to the clr module instead of asking us to do the load.  You should be able to do that via PythonEngine.LoadAssembly.


From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Matthew Swank
Sent: Tuesday, April 03, 2007 7:40 AM
To: 'Discussion of IronPython'
Subject: Re: [IronPython] strange runtime error hosting Iron Python in C# app

Also, using the library directly from the ipy shell does not trigger the observed behavior.

Matt
________________________________
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Dino Viehland
Sent: Monday, April 02, 2007 5:37 PM
To: Discussion of IronPython
Subject: Re: [IronPython] strange runtime error hosting Iron Python in C# app

How exactly are you doing this?
...

I have a C# app that embeds python to run scripts.

Given a script both the script and the app need to access the same library. Both the script and the app create objects using the library.  The app links to the shared library.  In addition, each hosted script has a preamble to add a reference to, and imports namespaces from the shared library.

Matt
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Matthew Swank
Sent: Monday, April 02, 2007 3:03 PM
To: users at lists.ironpython.com
Subject: [IronPython] strange runtime error hosting Iron Python in C# app

Given a set of classes in a c# Library like the following:

    public interface IFoo
    {
        int Bar { get;}
    }

    public abstract class FooBase : IFoo
    {
        public abstract int Bar { get;}
    }

    public class FooConcrete1 : FooBase
    {
        public override int Bar { get { return 1; } }
    }

and a C# consumer:
       public class FooUser
    {
        public static int FooInt(IFoo a)
        {
            return a.Bar;
        }
    }

if I call FooInt from Python I'll get: "We Encountered an error: expected IFoo, got FooConcrete1".

I suspect the error stems from adding an assembly as a reference inside the python code in addition to referencing it in the C# project.  If this is the case, how do I make referenced assemblies visible to the hosted python module w/o using AddReference...?

Thanks,

Matt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20070403/28a1c143/attachment.html>


More information about the Ironpython-users mailing list