Component.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // System.ComponentModel.Component.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Ximian, Inc. http://www.ximian.com
  9. // (C) 2003 Andreas Nahr
  10. //
  11. using System;
  12. using System.ComponentModel;
  13. namespace System.ComponentModel {
  14. [DesignerCategory ("Component")]
  15. public class Component : MarshalByRefObject, IComponent, IDisposable
  16. {
  17. private EventHandlerList event_handlers;
  18. private ISite mySite;
  19. private object disposedEvent = new object ();
  20. public Component ()
  21. {
  22. event_handlers = null;
  23. }
  24. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  25. public IContainer Container {
  26. get {
  27. if (mySite == null)
  28. return null;
  29. return mySite.Container;
  30. }
  31. }
  32. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  33. protected bool DesignMode {
  34. get {
  35. if (mySite == null)
  36. return false;
  37. return mySite.DesignMode;
  38. }
  39. }
  40. protected EventHandlerList Events {
  41. get {
  42. // Note: space vs. time tradeoff
  43. // We create the object here if it's never be accessed before. This potentially
  44. // saves space. However, we must check each time the propery is accessed to
  45. // determine whether we need to create the object, which increases overhead.
  46. // We could put the creation in the contructor, but that would waste space
  47. // if it were never used. However, accessing this property would be faster.
  48. if (null == event_handlers)
  49. event_handlers = new EventHandlerList();
  50. return event_handlers;
  51. }
  52. }
  53. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  54. public virtual ISite Site {
  55. get { return mySite; }
  56. set { mySite = value; }
  57. }
  58. ~Component()
  59. {
  60. Dispose (false);
  61. }
  62. public void Dispose ()
  63. {
  64. Dispose (true);
  65. GC.SuppressFinalize (this);
  66. }
  67. // <summary>
  68. // Controls disposal of resources used by this.
  69. // </summary>
  70. //
  71. // <param name="release_all"> Controls which resources are released</param>
  72. //
  73. // <remarks>
  74. // if release_all is set to true, both managed and unmanaged
  75. // resources should be released. If release_all is set to false,
  76. // only unmanaged resources should be disposed
  77. // </remarks>
  78. protected virtual void Dispose (bool release_all)
  79. {
  80. if (release_all) {
  81. EventHandler eh = (EventHandler) Events [disposedEvent];
  82. if (eh != null)
  83. eh (this, EventArgs.Empty);
  84. }
  85. mySite = null;
  86. }
  87. protected virtual object GetService (Type service)
  88. {
  89. if (mySite != null) {
  90. return mySite.GetService(service);
  91. }
  92. return null;
  93. }
  94. public override string ToString ()
  95. {
  96. if (mySite == null)
  97. return GetType ().ToString ();
  98. return String.Format ("{0} [{1}]", mySite.Name, GetType ().ToString ());
  99. }
  100. [Browsable (false), EditorBrowsable (EditorBrowsableState.Advanced)]
  101. public event EventHandler Disposed {
  102. add { Events.AddHandler (disposedEvent, value); }
  103. remove { Events.RemoveHandler (disposedEvent, value); }
  104. }
  105. }
  106. }