Component.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.ComponentModel;
  33. namespace System.ComponentModel {
  34. [DesignerCategory ("Component")]
  35. public class Component : MarshalByRefObject, IComponent, IDisposable
  36. {
  37. private EventHandlerList event_handlers;
  38. private ISite mySite;
  39. private object disposedEvent = new object ();
  40. public Component ()
  41. {
  42. event_handlers = null;
  43. }
  44. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  45. public IContainer Container {
  46. get {
  47. if (mySite == null)
  48. return null;
  49. return mySite.Container;
  50. }
  51. }
  52. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  53. protected bool DesignMode {
  54. get {
  55. if (mySite == null)
  56. return false;
  57. return mySite.DesignMode;
  58. }
  59. }
  60. protected EventHandlerList Events {
  61. get {
  62. // Note: space vs. time tradeoff
  63. // We create the object here if it's never be accessed before. This potentially
  64. // saves space. However, we must check each time the propery is accessed to
  65. // determine whether we need to create the object, which increases overhead.
  66. // We could put the creation in the contructor, but that would waste space
  67. // if it were never used. However, accessing this property would be faster.
  68. if (null == event_handlers)
  69. event_handlers = new EventHandlerList();
  70. return event_handlers;
  71. }
  72. }
  73. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  74. public virtual ISite Site {
  75. get { return mySite; }
  76. set { mySite = value; }
  77. }
  78. ~Component()
  79. {
  80. Dispose (false);
  81. }
  82. public void Dispose ()
  83. {
  84. Dispose (true);
  85. GC.SuppressFinalize (this);
  86. }
  87. // <summary>
  88. // Controls disposal of resources used by this.
  89. // </summary>
  90. //
  91. // <param name="release_all"> Controls which resources are released</param>
  92. //
  93. // <remarks>
  94. // if release_all is set to true, both managed and unmanaged
  95. // resources should be released. If release_all is set to false,
  96. // only unmanaged resources should be disposed
  97. // </remarks>
  98. protected virtual void Dispose (bool release_all)
  99. {
  100. if (release_all) {
  101. EventHandler eh = (EventHandler) Events [disposedEvent];
  102. if (eh != null)
  103. eh (this, EventArgs.Empty);
  104. }
  105. mySite = null;
  106. }
  107. protected virtual object GetService (Type service)
  108. {
  109. if (mySite != null) {
  110. return mySite.GetService(service);
  111. }
  112. return null;
  113. }
  114. public override string ToString ()
  115. {
  116. if (mySite == null)
  117. return GetType ().ToString ();
  118. return String.Format ("{0} [{1}]", mySite.Name, GetType ().ToString ());
  119. }
  120. [Browsable (false), EditorBrowsable (EditorBrowsableState.Advanced)]
  121. public event EventHandler Disposed {
  122. add { Events.AddHandler (disposedEvent, value); }
  123. remove { Events.RemoveHandler (disposedEvent, value); }
  124. }
  125. }
  126. }