The Labs \ Source Viewer \ SSCLI \ System.ComponentModel \ EventHandlerList

  1. //------------------------------------------------------------------------------
  2. // <copyright file="EventHandlerList.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.ComponentModel
  16. {
  17.     using System;
  18.     using System.Diagnostics.CodeAnalysis;
  19.     using System.Security.Permissions;
  20.    
  21.     /// <devdoc>
  22.     /// <para>Provides a simple list of delegates. This class cannot be inherited.</para>
  23.     /// </devdoc>
  24.     [HostProtection(SharedState = true)]
  25.     public sealed class EventHandlerList : IDisposable
  26.     {
  27.        
  28.         ListEntry head;
  29.         Component parent;
  30.        
  31.         /// <devdoc>
  32.         /// Creates a new event handler list.
  33.         /// </devdoc>
  34.         [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  35.         public EventHandlerList()
  36.         {
  37.         }
  38.        
  39.         /// <devdoc>
  40.         /// Creates a new event handler list. The parent component is used to check the component's
  41.         /// CanRaiseEvents property.
  42.         /// </devdoc>
  43.         [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  44.         internal EventHandlerList(Component parent)
  45.         {
  46.             this.parent = parent;
  47.         }
  48.        
  49.         /// <devdoc>
  50.         /// <para>Gets or sets the delegate for the specified key.</para>
  51.         /// </devdoc>
  52.         public Delegate this[object key]
  53.         {
  54.             [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  55.             get {
  56.                 ListEntry e = null;
  57.                 if (parent == null || parent.CanRaiseEventsInternal) {
  58.                     e = Find(key);
  59.                 }
  60.                 if (e != null) {
  61.                     return e.handler;
  62.                 }
  63.                 else {
  64.                     return null;
  65.                 }
  66.             }
  67.             set {
  68.                 ListEntry e = Find(key);
  69.                 if (e != null) {
  70.                     e.handler = value;
  71.                 }
  72.                 else {
  73.                     head = new ListEntry(key, value, head);
  74.                 }
  75.             }
  76.         }
  77.        
  78.         /// <devdoc>
  79.         /// <para>[To be supplied.]</para>
  80.         /// </devdoc>
  81.         [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  82.         public void AddHandler(object key, Delegate value)
  83.         {
  84.             ListEntry e = Find(key);
  85.             if (e != null) {
  86.                 e.handler = Delegate.Combine(e.handler, value);
  87.             }
  88.             else {
  89.                 head = new ListEntry(key, value, head);
  90.             }
  91.         }
  92.        
  93.         /// <devdoc> allows you to add a list of events to this list </devdoc>
  94.         public void AddHandlers(EventHandlerList listToAddFrom)
  95.         {
  96.            
  97.             ListEntry currentListEntry = listToAddFrom.head;
  98.             while (currentListEntry != null) {
  99.                 AddHandler(currentListEntry.key, currentListEntry.handler);
  100.                 currentListEntry = currentListEntry.next;
  101.             }
  102.         }
  103.        
  104.         /// <devdoc>
  105.         /// <para>[To be supplied.]</para>
  106.         /// </devdoc>
  107.         public void Dispose()
  108.         {
  109.             head = null;
  110.         }
  111.        
  112.         private ListEntry Find(object key)
  113.         {
  114.             ListEntry found = head;
  115.             while (found != null) {
  116.                 if (found.key == key) {
  117.                     break;
  118.                 }
  119.                 found = found.next;
  120.             }
  121.             return found;
  122.         }
  123.        
  124.         /// <devdoc>
  125.         /// <para>[To be supplied.]</para>
  126.         /// </devdoc>
  127.         [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  128.         public void RemoveHandler(object key, Delegate value)
  129.         {
  130.             ListEntry e = Find(key);
  131.             if (e != null) {
  132.                 e.handler = Delegate.Remove(e.handler, value);
  133.             }
  134.             // else... no error for removal of non-existant delegate
  135.             //
  136.         }
  137.        
  138.         private sealed class ListEntry
  139.         {
  140.             internal ListEntry next;
  141.             internal object key;
  142.             internal Delegate handler;
  143.            
  144.             public ListEntry(object key, Delegate handler, ListEntry next)
  145.             {
  146.                 this.next = next;
  147.                 this.key = key;
  148.                 this.handler = handler;
  149.             }
  150.         }
  151.     }
  152.    
  153.    
  154. }

Developer Fusion