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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="Component.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.ComponentModel.Design;
  19.     using System.ComponentModel.Design.Serialization;
  20.     using System.Runtime.InteropServices;
  21.     using System.Security.Permissions;
  22.    
  23.     /// <devdoc>
  24.     /// <para>Provides the default implementation for the
  25.     /// <see cref='System.ComponentModel.IComponent'/>
  26.     /// interface and enables object-sharing between applications.</para>
  27.     /// </devdoc>
  28.     [ComVisible(true), ClassInterface(ClassInterfaceType.AutoDispatch), DesignerCategory("Component")]
  29.     public class Component : MarshalByRefObject, IComponent
  30.     {
  31.        
  32.         /// <devdoc>
  33.         /// <para>Static hask key for the Disposed event. This field is read-only.</para>
  34.         /// </devdoc>
  35.         private static readonly object EventDisposed = new object();
  36.        
  37.         private ISite site;
  38.         private EventHandlerList events;
  39.        
  40.         ~Component()
  41.         {
  42.             Dispose(false);
  43.         }
  44.        
  45.         /// <devdoc>
  46.         /// This property returns true if the component is in a mode that supports
  47.         /// raising events. By default, components always support raising their events
  48.         /// and therefore this method always returns true. You can override this method
  49.         /// in a deriving class and change it to return false when needed. if the return
  50.         /// value of this method is false, the EventList collection returned by the Events
  51.         /// property will always return null for an event. Events can still be added and
  52.         /// removed from the collection, but retrieving them through the collection's Item
  53.         /// property will always return null.
  54.         /// </devdoc>
  55.         protected virtual bool CanRaiseEvents {
  56.             get { return true; }
  57.         }
  58.        
  59.         /// <devdoc>
  60.         /// Internal API that allows the event handler list class to access the
  61.         /// CanRaiseEvents property.
  62.         /// </devdoc>
  63.         internal bool CanRaiseEventsInternal {
  64.             get { return CanRaiseEvents; }
  65.         }
  66.        
  67.         /// <devdoc>
  68.         /// <para>Adds a event handler to listen to the Disposed event on the component.</para>
  69.         /// </devdoc>
  70.         [Browsable(false), EditorBrowsable(EditorBrowsableState.Advanced)]
  71.         public event EventHandler Disposed {
  72.             add { Events.AddHandler(EventDisposed, value); }
  73.             remove { Events.RemoveHandler(EventDisposed, value); }
  74.         }
  75.        
  76.         /// <devdoc>
  77.         /// <para>Gets the list of event handlers that are attached to this component.</para>
  78.         /// </devdoc>
  79.         protected EventHandlerList Events {
  80.             get {
  81.                 if (events == null) {
  82.                     events = new EventHandlerList(this);
  83.                 }
  84.                 return events;
  85.             }
  86.         }
  87.        
  88.         /// <devdoc>
  89.         /// <para>
  90.         /// Gets or sets the site of the <see cref='System.ComponentModel.Component'/>
  91.         /// .
  92.         /// </para>
  93.         /// </devdoc>
  94.         [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  95.         public virtual ISite Site {
  96.             get { return site; }
  97.             set { site = value; }
  98.         }
  99.        
  100.         /// <devdoc>
  101.         /// <para>
  102.         /// Disposes of the <see cref='System.ComponentModel.Component'/>
  103.         /// .
  104.         /// </para>
  105.         /// </devdoc>
  106.         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2213:DisposableFieldsShouldBeDisposed")]
  107.         public void Dispose()
  108.         {
  109.             Dispose(true);
  110.             GC.SuppressFinalize(this);
  111.         }
  112.        
  113.         /// <devdoc>
  114.         /// <para>
  115.         /// Disposes all the resources associated with this component.
  116.         /// If disposing is false then you must never touch any other
  117.         /// managed objects, as they may already be finalized. When
  118.         /// in this state you should dispose any native resources
  119.         /// that you have a reference to.
  120.         /// </para>
  121.         /// <para>
  122.         /// When disposing is true then you should dispose all data
  123.         /// and objects you have references to. The normal implementation
  124.         /// of this method would look something like:
  125.         /// </para>
  126.         /// <code>
  127.         /// public void Dispose() {
  128.         /// Dispose(true);
  129.         /// GC.SuppressFinalize(this);
  130.         /// }
  131.         ///
  132.         /// protected virtual void Dispose(bool disposing) {
  133.         /// if (disposing) {
  134.         /// if (myobject != null) {
  135.         /// myobject.Dispose();
  136.         /// myobject = null;
  137.         /// }
  138.         /// }
  139.         /// if (myhandle != IntPtr.Zero) {
  140.         /// NativeMethods.Release(myhandle);
  141.         /// myhandle = IntPtr.Zero;
  142.         /// }
  143.         /// }
  144.         ///
  145.         /// ~MyClass() {
  146.         /// Dispose(false);
  147.         /// }
  148.         /// </code>
  149.         /// <para>
  150.         /// For base classes, you should never override the Finalier (~Class in C#)
  151.         /// or the Dispose method that takes no arguments, rather you should
  152.         /// always override the Dispose method that takes a bool.
  153.         /// </para>
  154.         /// <code>
  155.         /// protected override void Dispose(bool disposing) {
  156.         /// if (disposing) {
  157.         /// if (myobject != null) {
  158.         /// myobject.Dispose();
  159.         /// myobject = null;
  160.         /// }
  161.         /// }
  162.         /// if (myhandle != IntPtr.Zero) {
  163.         /// NativeMethods.Release(myhandle);
  164.         /// myhandle = IntPtr.Zero;
  165.         /// }
  166.         /// base.Dispose(disposing);
  167.         /// }
  168.         /// </code>
  169.         /// </devdoc>
  170.         protected virtual void Dispose(bool disposing)
  171.         {
  172.             if (disposing) {
  173.                 lock (this) {
  174.                     if (site != null && site.Container != null) {
  175.                         site.Container.Remove(this);
  176.                     }
  177.                     if (events != null) {
  178.                         EventHandler handler = (EventHandler)events[EventDisposed];
  179.                         if (handler != null)
  180.                             handler(this, EventArgs.Empty);
  181.                     }
  182.                 }
  183.             }
  184.         }
  185.        
  186.         // Returns the component's container.
  187.         //
  188.         /// <devdoc>
  189.         /// <para>
  190.         /// Returns the <see cref='System.ComponentModel.IContainer'/>
  191.         /// that contains the <see cref='System.ComponentModel.Component'/>
  192.         /// .
  193.         /// </para>
  194.         /// </devdoc>
  195.         [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  196.         public IContainer Container {
  197.             get {
  198.                 ISite s = site;
  199.                 return s == null ? null : s.Container;
  200.             }
  201.         }
  202.        
  203.         /// <devdoc>
  204.         /// <para>
  205.         /// Returns an object representing a service provided by
  206.         /// the <see cref='System.ComponentModel.Component'/>
  207.         /// .
  208.         /// </para>
  209.         /// </devdoc>
  210.         protected virtual object GetService(Type service)
  211.         {
  212.             ISite s = site;
  213.             return ((s == null) ? null : s.GetService(service));
  214.         }
  215.        
  216.         /// <devdoc>
  217.         /// <para>
  218.         /// Gets a value indicating whether the <see cref='System.ComponentModel.Component'/>
  219.         /// is currently in design mode.
  220.         /// </para>
  221.         /// </devdoc>
  222.         [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  223.         protected bool DesignMode {
  224.             get {
  225.                 ISite s = site;
  226.                 return (s == null) ? false : s.DesignMode;
  227.             }
  228.         }
  229.        
  230.         /// <internalonly/>
  231.         /// <devdoc>
  232.         /// <para>
  233.         /// Returns a <see cref='System.String'/> containing the name of the <see cref='System.ComponentModel.Component'/> , if any. This method should not be
  234.         /// overridden. For
  235.         /// internal use only.
  236.         /// </para>
  237.         /// </devdoc>
  238.         public override string ToString()
  239.         {
  240.             ISite s = site;
  241.            
  242.             if (s != null)
  243.                 return s.Name + " [" + GetType().FullName + "]";
  244.             else
  245.                 return GetType().FullName;
  246.         }
  247.     }
  248. }

Developer Fusion