Observable.FromEvent<KeyEventHandler, KeyEventArgs>(e => textBox1.KeyUp += e, e => textBox1.KeyUp -= e)
Yuck. What's the deal with this? Why do we have to write out this boilerplate? Well, we don't, we also have the option of writing this:
Observable.FromEventPattern(textBox1, "KeyUp")
But this has its own drawbacks: there's no type information for the event (you still have to add that manually if you want it) and it uses reflection to get at the event (no compile-time checking and a potential performance problem if you do this more than once).
The problem is that events are not first-class citizens in C#, but syntactic sugar for automagically implemented delegate properties. It would be nice if events at least offered a way of getting type information at compile time, but they don't. The only operations available on an event are adding a handler (through +=) and removing one (through -=). If nothing else, C# 5 should at least strongly consider offering another operation: .ToObservable(). Then you could write:
textBox.KeyUp.ToObservable()
Which would just be syntactic sugar for Observable.FromEvent(), with the compiler filling in the boilerplate. I'm not sure if this can be made to work properly for events that flout convention and don't utilize EventArgs, but EventArgs should have full support from the .ToObservable()-conversion.
No comments:
Post a Comment