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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="MarshalByValueComponent.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.    
  18.     using System;
  19.     using System.ComponentModel.Design;
  20.     using System.Diagnostics.CodeAnalysis;
  21.     using System.Security.Permissions;
  22.     using System.Runtime.InteropServices;
  23.    
  24.     /// <devdoc>
  25.     /// <para>Provides the base implementation for <see cref='System.ComponentModel.IComponent'/>,
  26.     /// which is the base class for all components in Win Forms.</para>
  27.     /// </devdoc>
  28.     [ComVisible(true), Designer("System.Windows.Forms.Design.ComponentDocumentDesigner, " + AssemblyRef.SystemDesign, typeof(IRootDesigner)), DesignerCategory("Component"), TypeConverter(typeof(ComponentConverter))]
  29.     public class MarshalByValueComponent : IComponent, IServiceProvider
  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.         /// <devdoc>
  41.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.MarshalByValueComponent'/> class.</para>
  42.         /// </devdoc>
  43.         public MarshalByValueComponent()
  44.         {
  45.         }
  46.        
  47.         ~MarshalByValueComponent()
  48.         {
  49.             Dispose(false);
  50.         }
  51.        
  52.         /// <devdoc>
  53.         /// <para>Adds a event handler to listen to the Disposed event on the component.</para>
  54.         /// </devdoc>
  55.         public event EventHandler Disposed {
  56.             add { Events.AddHandler(EventDisposed, value); }
  57.             remove { Events.RemoveHandler(EventDisposed, value); }
  58.         }
  59.        
  60.         /// <devdoc>
  61.         /// <para>Gets the list of event handlers that are attached to this component.</para>
  62.         /// </devdoc>
  63.         protected EventHandlerList Events {
  64.             get {
  65.                 if (events == null) {
  66.                     events = new EventHandlerList();
  67.                 }
  68.                 return events;
  69.             }
  70.         }
  71.        
  72.         /// <devdoc>
  73.         /// <para>Gets or sets the site of the component.</para>
  74.         /// </devdoc>
  75.         [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  76.         public virtual ISite Site {
  77.             get { return site; }
  78.             set { site = value; }
  79.         }
  80.        
  81.         /// <devdoc>
  82.         /// <para>Disposes of the resources (other than memory) used by the component.</para>
  83.         /// </devdoc>
  84.         [SuppressMessage("Microsoft.Usage", "CA2213:DisposableFieldsShouldBeDisposed")]
  85.         public void Dispose()
  86.         {
  87.             Dispose(true);
  88.             GC.SuppressFinalize(this);
  89.         }
  90.        
  91.         /// <devdoc>
  92.         /// <para>
  93.         /// Disposes all the resources associated with this component.
  94.         /// If disposing is false then you must never touch any other
  95.         /// managed objects, as they may already be finalized. When
  96.         /// in this state you should dispose any native resources
  97.         /// that you have a reference to.
  98.         /// </para>
  99.         /// <para>
  100.         /// When disposing is true then you should dispose all data
  101.         /// and objects you have references to. The normal implementation
  102.         /// of this method would look something like:
  103.         /// </para>
  104.         /// <code>
  105.         /// public void Dispose() {
  106.         /// Dispose(true);
  107.         /// GC.SuppressFinalize(this);
  108.         /// }
  109.         ///
  110.         /// protected virtual void Dispose(bool disposing) {
  111.         /// if (disposing) {
  112.         /// if (myobject != null) {
  113.         /// myobject.Dispose();
  114.         /// myobject = null;
  115.         /// }
  116.         /// }
  117.         /// if (myhandle != IntPtr.Zero) {
  118.         /// NativeMethods.Release(myhandle);
  119.         /// myhandle = IntPtr.Zero;
  120.         /// }
  121.         /// }
  122.         ///
  123.         /// ~MyClass() {
  124.         /// Dispose(false);
  125.         /// }
  126.         /// </code>
  127.         /// <para>
  128.         /// For base classes, you should never override the Finalier (~Class in C#)
  129.         /// or the Dispose method that takes no arguments, rather you should
  130.         /// always override the Dispose method that takes a bool.
  131.         /// </para>
  132.         /// <code>
  133.         /// protected override void Dispose(bool disposing) {
  134.         /// if (disposing) {
  135.         /// if (myobject != null) {
  136.         /// myobject.Dispose();
  137.         /// myobject = null;
  138.         /// }
  139.         /// }
  140.         /// if (myhandle != IntPtr.Zero) {
  141.         /// NativeMethods.Release(myhandle);
  142.         /// myhandle = IntPtr.Zero;
  143.         /// }
  144.         /// base.Dispose(disposing);
  145.         /// }
  146.         /// </code>
  147.         /// </devdoc>
  148.         protected virtual void Dispose(bool disposing)
  149.         {
  150.             if (disposing) {
  151.                 lock (this) {
  152.                     if (site != null && site.Container != null) {
  153.                         site.Container.Remove(this);
  154.                     }
  155.                     if (events != null) {
  156.                         EventHandler handler = (EventHandler)events[EventDisposed];
  157.                         if (handler != null)
  158.                             handler(this, EventArgs.Empty);
  159.                     }
  160.                 }
  161.             }
  162.         }
  163.        
  164.         /// <devdoc>
  165.         /// <para>Gets the container for the component.</para>
  166.         /// </devdoc>
  167.         [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  168.         public virtual IContainer Container {
  169.             get {
  170.                 ISite s = site;
  171.                 return s == null ? null : s.Container;
  172.             }
  173.         }
  174.        
  175.         /// <devdoc>
  176.         /// <para>Gets the implementer of the <see cref='System.IServiceProvider'/>.</para>
  177.         /// </devdoc>
  178.         public virtual object GetService(Type service)
  179.         {
  180.             return ((site == null) ? null : site.GetService(service));
  181.         }
  182.        
  183.        
  184.         /// <devdoc>
  185.         /// <para>Gets a value indicating whether the component is currently in design mode.</para>
  186.         /// </devdoc>
  187.         [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  188.         public virtual bool DesignMode {
  189.             get {
  190.                 ISite s = site;
  191.                 return (s == null) ? false : s.DesignMode;
  192.             }
  193.         }
  194.         /// <internalonly/>
  195.         /// <devdoc>
  196.         /// <para>
  197.         /// Returns a <see cref='System.String'/> containing the name of the <see cref='System.ComponentModel.Component'/> , if any. This method should not be
  198.         /// overridden. For
  199.         /// internal use only.
  200.         /// </para>
  201.         /// </devdoc>
  202.         public override string ToString()
  203.         {
  204.             ISite s = site;
  205.            
  206.             if (s != null)
  207.                 return s.Name + " [" + GetType().FullName + "]";
  208.             else
  209.                 return GetType().FullName;
  210.         }
  211.     }
  212. }

Developer Fusion