How can I get an IObservable<T> in Rx from a "non-standard" event?

Posted by Dan Tao on Stack Overflow See other posts from Stack Overflow or by Dan Tao
Published on 2010-05-26T16:49:27Z Indexed on 2010/05/26 16:51 UTC
Read the original article Hit count: 260

Filed under:
|
|
|
|

Here's what I mean. Suppose I'm working with an API that exposes events, but these events do not follow the standard EventHandler or EventHandler<TEventArgs> signature. One event might look like this, for instance:

Public Event Update(ByVal sender As BaseSubscription, ByVal e As BaseEvent)

Now, typically, if I want to get an IObservable<TEventArgs> from an event, I can just do this:

Dim updates = Observable.FromEvent(Of UpdateEventArgs)( _
    target:=updateSource, _
    eventName:="Update" _
)

But this doesn't work, because the Update event is not an EventHandler<UpdateEventArgs> -- in fact, there is no UpdateEventArgs -- it's basically just its own thing.

Obviously, I could define my own class deriving from EventArgs (i.e., UpdateEventArgs), write another class to wrap the object providing the Update event, give the wrapper class its own Update event that is an EventHandler<UpdateEventArgs>, and get an IObservable<UpdateEventArgs> from that. But that's an annoying amount of work.

Is there some way to create an IObservable<[something]> from a "non-standard" event like this, or am I out of luck?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about vb.net