ServiceContainer.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // System.ComponentModel.Design.ServiceContainer.cs
  3. //
  4. // Authors:
  5. // Martin Willemoes Hansen ([email protected])
  6. // Andreas Nahr ([email protected])
  7. // Ivan N. Zlatev (contact i-nZ.net)
  8. //
  9. // (C) 2003 Martin Willemoes Hansen
  10. // (C) 2003 Andreas Nahr
  11. // (C) 2006 Ivan N. Zlatev
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.Collections;
  34. namespace System.ComponentModel.Design
  35. {
  36. #if NET_2_0
  37. public class ServiceContainer : IServiceContainer, IServiceProvider, IDisposable
  38. #else
  39. public sealed class ServiceContainer : IServiceContainer, IServiceProvider
  40. #endif
  41. {
  42. private IServiceProvider parentProvider;
  43. private Hashtable services = new Hashtable ();
  44. #if NET_2_0
  45. private bool _disposed = false;
  46. #endif
  47. public ServiceContainer()
  48. : this (null)
  49. {
  50. }
  51. public ServiceContainer (IServiceProvider parentProvider)
  52. {
  53. this.parentProvider = parentProvider;
  54. }
  55. public void AddService (Type serviceType, object serviceInstance)
  56. {
  57. AddService (serviceType, serviceInstance, false);
  58. }
  59. public void AddService (Type serviceType, ServiceCreatorCallback callback)
  60. {
  61. AddService (serviceType, callback, false);
  62. }
  63. #if NET_2_0
  64. public virtual
  65. #else
  66. public
  67. #endif
  68. void AddService (Type serviceType,
  69. object serviceInstance,
  70. bool promote)
  71. {
  72. if (serviceType == null)
  73. throw new ArgumentNullException ("serviceType", "Cannot be null");
  74. if (promote)
  75. if (parentProvider != null)
  76. ((IServiceContainer)parentProvider.GetService(typeof(IServiceContainer))).AddService (serviceType, serviceInstance, promote);
  77. if (services.Contains (serviceType)) {
  78. throw new ArgumentException (string.Format ("The service {0} already exists in the service container.", serviceType.ToString()));
  79. }
  80. services.Add (serviceType, serviceInstance);
  81. }
  82. #if NET_2_0
  83. public virtual
  84. #else
  85. public
  86. #endif
  87. void AddService (Type serviceType,
  88. ServiceCreatorCallback callback,
  89. bool promote)
  90. {
  91. if (serviceType == null)
  92. throw new ArgumentNullException ("serviceType", "Cannot be null");
  93. if (promote)
  94. if (parentProvider != null)
  95. ((IServiceContainer)parentProvider.GetService(typeof(IServiceContainer))).AddService (serviceType, callback, promote);
  96. if (services.Contains (serviceType)) {
  97. throw new ArgumentException (string.Format ("The service {0} already exists in the service container.", serviceType.ToString()));
  98. }
  99. services.Add (serviceType, callback);
  100. }
  101. public void RemoveService (Type serviceType)
  102. {
  103. RemoveService (serviceType, false);
  104. }
  105. #if NET_2_0
  106. public virtual void RemoveService (Type serviceType, bool promote)
  107. #else
  108. public void RemoveService (Type serviceType, bool promote)
  109. #endif
  110. {
  111. if (serviceType == null)
  112. throw new ArgumentNullException ("serviceType", "Cannot be null");
  113. if (promote)
  114. if (parentProvider != null)
  115. ((IServiceContainer)parentProvider.GetService(typeof(IServiceContainer))).RemoveService (serviceType, promote);
  116. else
  117. services.Remove (serviceType);
  118. }
  119. #if NET_2_0
  120. public virtual
  121. #else
  122. public
  123. #endif
  124. object GetService (Type serviceType)
  125. {
  126. object result = services[serviceType];
  127. if (result == null && parentProvider != null){
  128. result = parentProvider.GetService (serviceType);
  129. }
  130. if (result != null) {
  131. ServiceCreatorCallback cb = result as ServiceCreatorCallback;
  132. if (cb != null) {
  133. result = cb (this, serviceType);
  134. services[serviceType] = result;
  135. }
  136. }
  137. return result;
  138. }
  139. #if NET_2_0
  140. protected virtual Type [] DefaultServices {
  141. get { return new Type [] { typeof (IServiceContainer), typeof (ServiceContainer)}; }
  142. }
  143. public void Dispose ()
  144. {
  145. this.Dispose (true);
  146. GC.SuppressFinalize(this);
  147. }
  148. protected virtual void Dispose (bool disposing)
  149. {
  150. if (!_disposed) {
  151. if (disposing) {
  152. if (this.services != null) {
  153. foreach (object obj in this.services) {
  154. if (obj is IDisposable) {
  155. ((IDisposable) obj).Dispose ();
  156. }
  157. }
  158. this.services = null;
  159. }
  160. }
  161. _disposed = true;
  162. }
  163. }
  164. #endif
  165. }
  166. }