When creating an observable from an event, it looks like the following is the most common way:
var o = Observable.FromEventPattern(h => source.Event += h,
h => source.Event -= h);
I find this form a little tedious in some cases where I would like to handle multiple events in the same way. But that doesn't look to easy, since the event it self seems impossible to parameterize, as shown in this non-compiling code:
private IObservable MakeAnObservableFromThisEvent(Event event)
{
return Observable.FromEventPattern(h => event += h,
h => event -= h);
}
private void MakeAlotOfObservables(object source)
{
MakeAnObservableFromThisEvent(source.OneEvent);
MakeAnObservableFromThisEvent(source.AnotherEvent);
MakeAnObservableFromThisEvent(source.ThirdEvent);
//or even
MakeAnObservableFromThisEvent(() => source.ThirdEvent);
}
Of cause there is the 'event name'-overload:
var o = Observable.FromEventPattern< >(source, "Event");
but then there is this thing with more or less magic strings...
Is there away of optimizing this code? Or is this just the way things are?