[IronPython] IronPython and polymorphism? Help with interfaces

Haibo Luo haiboluo at exchange.microsoft.com
Wed Jan 23 18:00:29 CET 2008


Expect IPerson.Name.GetValue(person_object) to work (and IPerson.Name.__get__)

From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Miha Valencic
Sent: Wednesday, January 23, 2008 3:12 AM
To: users at lists.ironpython.com
Subject: [IronPython] IronPython and polymorphism? Help with interfaces

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<http://Wrapped.person.Name>");
       }
   }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20080123/19803710/attachment.html>


More information about the Ironpython-users mailing list