[IronPython] IronPython and polymorphism? Help with interfaces

Miha Valencic miha.valencic at gmail.com
Wed Jan 23 03:11:42 PST 2008


Hi!

I'm having troubles accessing .NET object properties from ipy. Object
is passed to IronPython 1.1 and IronPython breaks with error:
Unhandled Exception: System.MissingMemberException: 'Person' object
has no attribute 'Name'

This is all due to the fact, that this object is based on an
interface, which is based on an interface. To make things clearer I've
created a simple problem-statement program, which demonstrates the
behavior.

Problem: IronPython can NOT access class members of an explicitly
implemented interface. The program that exhibits this is below, and
also on http://www.mihavalencic.com/temp/Program.txt (for easier
reading). Just compile it with IronPython.dll and it will break where
it shouldn't (IMHO).

I was searching information on how could I explicitly cast this object
to IPerson (in the example), but could not find anyhing -- Python as a
language apparently does not support interfaces.

Ideas, suggestions are very welcome!

update: I even tried something like this:

person_explicit = IPerson("Wrapped.person);
print person_explicit.Name;

but I alwyas get the same error: Either the object does not have Name
property or that NoneType is not callable.

Thanks,
 Miha.

The progarm:
using System;
using IronPython.Hosting;
using IronPython.Modules;
using System.Collections.Generic;

namespace ProblemStatement
{
   public interface IPerson
   {
       string Name {get;set;}
   }
   public class Person : IPerson
   {
       string IPerson.Name
       {
           get
           {
               return "Default name";
           }
           set
           {
           }
       }
   }

   public class Wrapper
   {
       public IPerson person;
       public Wrapper(IPerson personIn)
       {
           person = personIn;
       }

   }

   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>();

           locals["Env"] = new Person();
           Wrapper wrapped = new Wrapper(new Person());
           locals["Wrapped"] = wrapped;

           // this works
           Console.WriteLine(wrapped.person.Name<http://wrapped.person.name/>
);

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

           // this breaks as well
           eng.Execute("print Wrapped.person.Name");
       }
   }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ironpython.com/pipermail/users-ironpython.com/attachments/20080123/472173aa/attachment.html 


More information about the Users mailing list