[IronPython] More fun with delegates

Dan Eloff dan.eloff at gmail.com
Thu Nov 20 13:20:39 PST 2008


This is not a blocker, but In RC1 and the 2.1 branch of 43741 the
following happens:

>>> import clr
>>> clr.AddReference('PresentationFramework')
>>> clr.AddReference('WindowsBase')
>>> from System.Windows import Window
>>> d = Window().Dispatcher
>>> def echo(x):
...     print x
...
>>> d.BeginInvoke(echo, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected Delegate, got function
>>> d.BeginInvoke(lambda: echo(2))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected Delegate, got function

However, in Silverlight, for some odd reason, it works fine.

>>> from System import Action
>>> def foo(): pass
>>> Action(foo)

Will crash the interpreter/browser. But this will work:

>>> def foo(i):
...     pass
...
>>> from System import Action
>>> Action[int](foo)
<System.Action`1[[System.Int32, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089]] object at
0x000000000000002B [System.Action`1[System.Int32]]>

So a workaround for now is to do:
>>> d.BeginInvoke(Action[object](echo), 2)

-Dan


More information about the Users mailing list