[IronPython] .NET Events in IronPython

Martin Maly Martin.Maly at microsoft.com
Tue Nov 15 02:23:23 CET 2005

After playing with this for a bit, I think that at the moment IronPython is not capable of what you can do below in C#. The insurmountable (for now, I hope) obsctacle that I hit is that the events must be sealed classes, inherited from an abstract MulticastDelegate. IronPython doesn't know of this rule and for now doesn't generate sealed class and the IronPython code that attempts to mimic the output of C# compiler throws exception "delegate must be a sealed class".

I think that as we improve the 'static compiler' story, we may need to support creating sealed classes. For now, the scenario below is broken...



From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Michael Shilman
Sent: Friday, November 11, 2005 12:59 PM
To: Discussion of IronPython
Subject: [IronPython] .NET Events in IronPython

Is there any way to declare .NET events in IronPython?

Here is a simple example in C# that "abstracts" a button pressed event into a "hello" event:

    public delegate void HelloInvoked();
    public class HelloControl : Panel
        public event HelloInvoked Hello;
        public HelloControl()
            Button b = new Button();
            b.Text = "Hello";
            b.Click += new EventHandler(b_Click);
            b.Dock = DockStyle.Fill;

        void b_Click(object sender, EventArgs e)
            if (Hello != null) Hello();

I can do something equivalent with Python functions:

class HelloControl(Panel):
     Hello = []
     def __init__(self):
          b = Button(Text="Hello",Dock=DockStyle.Fill)
          b.Click += self.on_Click
     def on_Click(self,sender,e):
          for h in self.Hello: h()
def Foo(): print "hello"
hc.Hello += [Foo]

But for consistency with my other C# code I'd like to use events if possible.

Any thoughts?

Many thanks,


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20051114/896a1336/attachment.html>

More information about the Ironpython-users mailing list