[IronPython] IronPython and polymorphism? Help with interfaces

Slide slide.o.mix at gmail.com
Wed Jan 23 13:13:06 CET 2008


What if you don't implement the interface explicitly? You have
IPerson.Name as the property name. That means that the object you need
to pass in to the IronPython MUST be an IPerson rather than any type
implementing IPerson. If you change it to just public string Name { }
instead, does that work?

>From an MSDN explicit interface tutorial

"A class that implements an interface can explicitly implement a
member of that interface. When a member is explicitly implemented, it
cannot be accessed through a class instance, but only through an
instance of the interface."

Which means you either need to not implement the interface explicitly
for that member, or you need to cast to IPerson before passing the
object in.

Thanks,

slide

On Jan 23, 2008 4:11 AM, Miha Valencic <miha.valencic at gmail.com> wrote:
> 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);
>
>             // this breaks
>             eng.Execute("print Env.Name", mod, locals);
>
>             // this breaks as well
>            eng.Execute("print Wrapped.person.Name");
>         }
>     }
> }
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>



-- 
slide-o-blog
http://slide-o-blog.blogspot.com/



More information about the Ironpython-users mailing list