[IronPython] .NET attributes for methods
Lukas Cenovsky
cenovsky at bakalari.cz
Fri Nov 13 17:26:09 CET 2009
Help with getting properties to work will hopefully resolve the
databinding as well ;-)
The property getter looks wrong but I have no idea what's wrong:
py> props = a.root.listbox1.Items[0].GetType().GetProperties()
py> props
=> Array[PropertyInfo]((<System.Reflection.RuntimePropertyInfo object at 0x000000000000002C [System.String name]>))
py> prop = props[0]
py> prop.GetGetMethod()
=> <System.Reflection.RuntimeMethodInfo object at 0x000000000000002B [System.String name()]>
py> prop.GetGetMethod().Invoke(a.root.listbox1.Items[0], None)
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
at <module> in <string>, line 0
Just for clarification "a" is Silverlight Application instance
As for the interface error - this is a different strory. You may
remember I created WCF service in IronPython with just interface defined
in C#. To be able to move interface to IronPython, I need such interface
to work with Silverlight too because Silverlight is the cause why I am
building WCF services.
--
-- Lukáš
Shri Borde wrote:
>
> I can help you create a property. And I can help you get it to work
> with your decorators (you can check if the "name" property exposes
> your @notify_property wrapper methods or not, and presumably that
> works). Beyond that, you are on your own :)
>
>
>
> I would try calling the property methods to make sure they are
> accessible. Something like this. If that works, I am not sure what
> Silverlight needs to make databinding happy.
>
>
>
> props = a.root.listbox1.Items[0].GetType().GetProperties()
>
> prop = props[0]
>
> prop.GetGetMethod.Invoke(a, None) # call using Reflection
>
>
>
> About the "SystemError: Application code cannot access
> System.AppDomain.get_CurrentDomain() using Reflection." error when
> defining interfaces, it could be worked around. We need to call
> AppDomain.DefineDynamicAssembly, and IronPython itself does do this.
> So its just a question of figuring out the right way to access an
> AppDomain instance. Will look into it, but I doubt it will help you
> with data binding.
>
>
>
> *From:* users-bounces at lists.ironpython.com
> [mailto:users-bounces at lists.ironpython.com] *On Behalf Of *Lukas Cenovsky
> *Sent:* Friday, November 13, 2009 5:42 AM
> *To:* Discussion of IronPython
> *Subject:* Re: [IronPython] .NET attributes for methods
>
>
>
> This looks very promising but I cannot make it work. I have changed
> product.py in DevHawk's example to:
>
> #from clrtypeold import ClrMetaclass
> import clrtype
>
> class Product(object):
> #__metaclass__ = ClrMetaclass
> __metaclass__ = clrtype.ClrClass
> _clrnamespace = "DevHawk.IronPython.ClrTypeSeries"
> #_clrproperties = {
> #"name":str,
> #"cost":float,
> #"quantity":int,
> #}
>
> def __init__(self, name, cost, quantity):
> self.name = name
> self.cost = cost
> self.quantity = quantity
>
> def calc_total(self):
> return self.cost * self.quantity
>
> @property
> @clrtype.accepts()
> @clrtype.returns(str)
> def name(self):
> return self._name
>
> @name.setter
> @clrtype.accepts(str)
> @clrtype.returns()
> def name(self, value):
> self._name = value
>
>
> When I run it I don't see any items in the listbox. When I check the
> name, it is a property:
>
> py> a.root.listbox1.Items[0]
> => <Product object at 0x000000000000002B>
> py> a.root.listbox1.Items[0].GetType().GetProperties()
> => Array[PropertyInfo]((<System.Reflection.RuntimePropertyInfo object at 0x000000000000002C [System.String name]>))
>
>
> Whe I used the old clrtype with _clrproperties = {'name': str, ...},
> it worked.
>
> --
> -- Lukáš
>
>
> Shri Borde wrote:
>
> Here is an updated version of clrtype.py that uses @property +
> @clrtype.accepts/@clrtype.returns to indicate a CLR property, instead
> of using "_clrproperties". I think its more Pythonic in general, but
> also you should be able to modify @notify_property to work with it.
>
>
>
> Note that notify_property won't just work. You will have to change it
> to propagate the func_name, arg_types, and return_type properties from
> the old getter/setter function objects to the new getter/setter
> function objects since these values are used by clrtype to generate
> the CLR members. Something like this:
>
>
>
> class notify_property(property):
>
>
>
> def propagate_attributes(old_function, new_function):
>
> new_function.func_name = old_function.func_name
>
> new_function.arg_types = old_function.arg_types
>
> new_function.return_type = old_function.return_type
>
>
>
> def __init__(self, getter):
>
> def newgetter(slf):
>
> try:
>
> return getter(slf)
>
> except AttributeError:
>
> return None
>
> propagate_attributes(getter, newgetter)
>
> super(notify_property, self).__init__(newgetter)
>
>
>
> def setter(self, setter):
>
> def newsetter(slf, newvalue):
>
> oldvalue = self.fget(slf)
>
> if oldvalue != newvalue:
>
> setter(slf, newvalue)
>
> slf.OnPropertyChanged(setter.__name__)
>
> propagate_attributes(setter, newsetter)
>
> return property(
>
> fget=self.fget,
>
> fset=newsetter,
>
> fdel=self.fdel,
>
> doc=self.__doc__)
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20091113/3b4fe2f2/attachment.html>
More information about the Ironpython-users
mailing list