[IronPython] dynamic types

Jonathan Jacobs korpse-ironpython at kaydash.za.net
Wed Aug 16 13:37:30 CEST 2006


Aviad Rozenhek wrote:
> what  I would like is to expose the "DynamicProperties" of my class as 
> "actual" properties in IP, which is logical since IP is dynamic.

I'm not sure if there is a way you can implement __getattr__ in your C# class 
(and have IronPython Do The Right Thing,) but you could do something like the 
following IronPython code:

import YourAssembly

class DynamicPropertiesContainer(YourAssembly.DynamicPropertiesContainer):
     def __getattr__(self, attr):
         try:
             return self.GetDynamicProperty(attr)
         except NoSuchDynamicProperty:
             raise AttributeError, "'%s' object has no attribute '%s'" % 
(type(self).__name__, attr)

Assuming of course your GetDynamicProperty method throws an exception when 
trying to get a property that doesn't exist, otherwise modify accordingly.
-- 
Jonathan

When you meet a master swordsman,
show him your sword.
When you meet a man who is not a poet,
do not show him your poem.
                 -- Rinzai, ninth century Zen master



More information about the Ironpython-users mailing list