Advertisement
Guest User

"Santa's Envoy"'s AsObservable Test

a guest
Mar 13th, 2010
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.84 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace System
  6. {
  7.     public static class DelegateEx
  8.     {
  9.         public static IObservable<IEvent<TEventArgs>> AsObservable<TEventArgs>(this EventHandler<TEventArgs> @event) where TEventArgs : EventArgs
  10.         {
  11.             return Observable.FromEvent<TEventArgs>(handler => @event += handler, handler => @event -= handler);
  12.         }
  13.     }
  14.  
  15.     public class Program
  16.     {
  17.         public static EventHandler<EventArgs> SomeEvent;
  18.  
  19.         static void Main()
  20.         {
  21.             var synthesis = new System.Speech.Synthesis.SpeechSynthesizer();
  22.  
  23.             SomeEvent.AsObservable().Subscribe(args => synthesis.Speak(args.Sender.ToString()));
  24.  
  25.             synthesis.Speak(SomeEvent == null ? "SomeEvent is null." : "SomeEvent is not null.");
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement