/// <summary>
        /// Converts a .NET event conforming to the standard .NET event pattern into an observable
        /// sequence, subscribing weakly.
        /// </summary>
        /// <typeparam name="TTarget">The type of target.</typeparam>
        /// <typeparam name="TEventArgs">The type of the event args.</typeparam>
        /// <param name="target">Object instance that exposes the event to convert.</param>
        /// <param name="eventName">Name of the event to convert.</param>
        /// <returns></returns>
        public static IObservable <EventPattern <object, TEventArgs> > FromEventPattern <TTarget, TEventArgs>(
            TTarget target,
            string eventName)
            where TEventArgs : EventArgs
        {
            Contract.Requires <ArgumentNullException>(target != null);
            Contract.Requires <ArgumentNullException>(eventName != null);

            return(Observable.Create <EventPattern <object, TEventArgs> >(observer =>
            {
                var handler = new Handler <TEventArgs>(observer);
                WeakSubscriptionManager.Subscribe(target, eventName, handler);
                return () => WeakSubscriptionManager.Unsubscribe(target, eventName, handler);
            }).Publish().RefCount());
        }
Example #2
0
        /// <summary>
        /// Converts a .NET event conforming to the standard .NET event pattern into an observable
        /// sequence, subscribing weakly.
        /// </summary>
        /// <typeparam name="TTarget">The type of target.</typeparam>
        /// <typeparam name="TEventArgs">The type of the event args.</typeparam>
        /// <param name="target">Object instance that exposes the event to convert.</param>
        /// <param name="eventName">Name of the event to convert.</param>
        /// <returns></returns>
        public static IObservable <EventPattern <object, TEventArgs> > FromEventPattern <TTarget, TEventArgs>(
            TTarget target,
            string eventName)
            where TEventArgs : EventArgs
        {
            _ = target ?? throw new ArgumentNullException(nameof(target));
            _ = eventName ?? throw new ArgumentNullException(nameof(eventName));

            return(Observable.Create <EventPattern <object, TEventArgs> >(observer =>
            {
                var handler = new Handler <TEventArgs>(observer);
                WeakSubscriptionManager.Subscribe(target, eventName, handler);
                return () => WeakSubscriptionManager.Unsubscribe(target, eventName, handler);
            }).Publish().RefCount());
        }