www.pudn.com > Code.zip > EventHandlerSet.cs, change:2002-01-07,size:3920b


/****************************************************************************** 
Module:  EventHandlerSet.cs 
Notices: Copyright (c) 2002 Jeffrey Richter 
******************************************************************************/ 
 
 
using System; 
using System.Collections; 
using System.Runtime.CompilerServices; 
 
 
/////////////////////////////////////////////////////////////////////////////// 
 
 
public class EventHandlerSet : IDisposable { 
 
   // The private hashtable collection used to maintain  
   // the event key/delegate value pairs 
   private Hashtable events = new Hashtable(); 
 
 
   // An index property that gets/sets the delegate associated 
   // with the passed event object's hashcode key 
   public virtual Delegate this[Object eventKey] { 
      // Returns null if object is not in set 
      get { return (Delegate) events[eventKey]; } 
      set { events[eventKey] = value; } 
   } 
 
    
   // Adds/Combines a delegate for the indicated event hashcode key 
   public virtual void AddHandler(Object eventKey, Delegate handler) { 
      events[eventKey] =  
         Delegate.Combine((Delegate) events[eventKey], handler); 
   } 
 
 
   // Removes a delegate for the indicated event hashcode key 
   public virtual void RemoveHandler(Object eventKey, Delegate handler) { 
      events[eventKey] =  
         Delegate.Remove((Delegate) events[eventKey], handler); 
   } 
 
 
   // Fires the delegate for the indicated event hashcode key 
   public virtual void Fire(Object eventKey, Object sender, EventArgs e) { 
      Delegate d = (Delegate) events[eventKey]; 
      if (d != null) d.DynamicInvoke(new Object[] { sender, e }); 
   } 
 
 
   // Disposing this object allows the memory used by the hashtable  
   // object to be reclaimed at the next garbage collection. 
   public void Dispose() { events = null; } 
    
 
   // This static method returns a thread-safe wrapper around the 
   // specified EventHandlerSet object 
   public static EventHandlerSet Synchronized( 
      EventHandlerSet eventHandlerSet) { 
 
      if (eventHandlerSet == null)  
         throw new ArgumentNullException("eventHandlerSet"); 
      return new SynchronizedEventHandlerSet(eventHandlerSet); 
   } 
    	 
 
   // This class is the thread-safe wrapper around  
   private class SynchronizedEventHandlerSet : EventHandlerSet { 
 
      // The reference to the thread-unsafe object 
      private EventHandlerSet eventHandlerSet; 
 
      // Construct a thread-safe wrapper over the thread-unsafe object 
      public SynchronizedEventHandlerSet( 
         EventHandlerSet eventHandlerSet) { 
 
         this.eventHandlerSet = eventHandlerSet; 
         Dispose();  // Let the base type's hashtable object be freed 
      } 
 
      // Thread-safe indexer property 
      public override Delegate this[Object eventKey] { 
         [MethodImpl(MethodImplOptions.Synchronized)] 
         get { return eventHandlerSet[eventKey]; } 
 
         [MethodImpl(MethodImplOptions.Synchronized)] 
         set { eventHandlerSet[eventKey] = value; } 
      } 
 
      // Thread-safe AddHandler method 
      [MethodImpl(MethodImplOptions.Synchronized)] 
      public override void AddHandler( 
         Object eventKey, Delegate handler) { 
         eventHandlerSet.AddHandler(eventKey, handler); 
      } 
 
 
      // Thread-safe RemoveHandler method 
      [MethodImpl(MethodImplOptions.Synchronized)] 
      public override void RemoveHandler( 
         Object eventKey, Delegate handler) { 
         eventHandlerSet.RemoveHandler(eventKey, handler); 
      } 
 
 
      // Thread-safe Fire method 
      [MethodImpl(MethodImplOptions.Synchronized)] 
      public override void Fire( 
         Object eventKey, Object sender, EventArgs e) { 
         eventHandlerSet.Fire(eventKey, sender, e); 
      } 
   } 
} 
 
 
//////////////////////////////// End of File //////////////////////////////////