[IronPython] IronPython and polymorphism? Help with interfaces

Slide slide.o.mix at gmail.com
Wed Jan 23 15:55:30 CET 2008


On Jan 23, 2008 6:46 AM, Miha Valencic <miha.valencic at gmail.com> wrote:
> Slide, thanks for a quick response. My answers are inline:
>
>
> On Jan 23, 2008 1:13 PM, Slide <slide.o.mix at gmail.com> wrote:
> > What if you don't implement the interface explicitly? You have
> >
>
> That of course works.
>
> >
> > >From an MSDN explicit interface tutorial
> >
> >
>
> Well, if you are the developer of the library in question this is one way to
> solve it. But in my case, this is really a 3rd party library that I want to
> access. And I can do so with .NET language, but no can do with Ipy. You
> mentioned explicit cast to IPerson. I think this is the language problem in
> python, since Python does not support interfaces. Thus, code
>
> explicit_cast = IPerson(objectInQuestion)
>
> throws an exception NoneType yada yada. Am I missing something here?
>
> Thanks,
>  Miha.
>


Try this:

class Program
   {
       static void Main(string[] args)
       {
           PythonEngine eng = new PythonEngine();
           EngineModule mod = eng.CreateModule();
           ClrModule clr = eng.Import("clr") as ClrModule;
           clr.AddReferenceByPartialName("ProblemStatement");

           Dictionary<string, object> locals = new Dictionary<string,
object>();
           Person p = new Person();
           locals["Env"] = p as IPerson;

           // this breaks
           eng.Execute("print Env.Name", mod, locals);
       }
   }
}

As you can see, I just made the reference in the locals an IPerson explicitly.



More information about the Ironpython-users mailing list