ServiceContainer.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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;
  44. #if NET_2_0
  45. private bool _disposed;
  46. #endif
  47. public ServiceContainer()
  48. : this (null)
  49. {
  50. }
  51. private Hashtable Services {
  52. get {
  53. if (services == null)
  54. services = new Hashtable ();
  55. return services;
  56. }
  57. }
  58. public ServiceContainer (IServiceProvider parentProvider)
  59. {
  60. this.parentProvider = parentProvider;
  61. }
  62. public void AddService (Type serviceType, object serviceInstance)
  63. {
  64. AddService (serviceType, serviceInstance, false);
  65. }
  66. public void AddService (Type serviceType, ServiceCreatorCallback callback)
  67. {
  68. AddService (serviceType, callback, false);
  69. }
  70. #if NET_2_0
  71. public virtual
  72. #else
  73. public
  74. #endif
  75. void AddService (Type serviceType,
  76. object serviceInstance,
  77. bool promote)
  78. {
  79. if (promote && parentProvider != null) {
  80. IServiceContainer container = (IServiceContainer)
  81. parentProvider.GetService (typeof (IServiceContainer));
  82. container.AddService (serviceType, serviceInstance, promote);
  83. return;
  84. }
  85. if (serviceType == null)
  86. throw new ArgumentNullException ("serviceType");
  87. if (serviceInstance == null)
  88. throw new ArgumentNullException ("serviceInstance");
  89. if (Services.Contains (serviceType))
  90. throw new ArgumentException (string.Format (
  91. "The service {0} already exists in the service container.",
  92. serviceType.ToString ()), "serviceType");
  93. Services.Add (serviceType, serviceInstance);
  94. }
  95. #if NET_2_0
  96. public virtual
  97. #else
  98. public
  99. #endif
  100. void AddService (Type serviceType,
  101. ServiceCreatorCallback callback,
  102. bool promote)
  103. {
  104. if (promote && parentProvider != null) {
  105. IServiceContainer container = (IServiceContainer)
  106. parentProvider.GetService (typeof (IServiceContainer));
  107. container.AddService (serviceType, callback, promote);
  108. return;
  109. }
  110. if (serviceType == null)
  111. throw new ArgumentNullException ("serviceType");
  112. if (callback == null)
  113. throw new ArgumentNullException ("callback");
  114. if (Services.Contains (serviceType))
  115. throw new ArgumentException (string.Format (
  116. "The service {0} already exists in the service container.",
  117. serviceType.ToString ()), "serviceType");
  118. Services.Add (serviceType, callback);
  119. }
  120. public void RemoveService (Type serviceType)
  121. {
  122. RemoveService (serviceType, false);
  123. }
  124. #if NET_2_0
  125. public virtual void RemoveService (Type serviceType, bool promote)
  126. #else
  127. public void RemoveService (Type serviceType, bool promote)
  128. #endif
  129. {
  130. if (promote && parentProvider != null) {
  131. IServiceContainer container = (IServiceContainer)
  132. parentProvider.GetService (typeof (IServiceContainer));
  133. container.RemoveService (serviceType, promote);
  134. return;
  135. }
  136. if (serviceType == null)
  137. throw new ArgumentNullException ("serviceType");
  138. Services.Remove (serviceType);
  139. }
  140. #if NET_2_0
  141. public virtual
  142. #else
  143. public
  144. #endif
  145. object GetService (Type serviceType)
  146. {
  147. object result = null;
  148. Type[] defaultServices = this.DefaultServices;
  149. for (int i=0; i < defaultServices.Length; i++) {
  150. if (defaultServices[i] == serviceType) {
  151. result = this;
  152. break;
  153. }
  154. }
  155. if (result == null)
  156. result = Services [serviceType];
  157. if (result == null && parentProvider != null)
  158. result = parentProvider.GetService (serviceType);
  159. if (result != null) {
  160. ServiceCreatorCallback cb = result as ServiceCreatorCallback;
  161. if (cb != null) {
  162. result = cb (this, serviceType);
  163. Services [serviceType] = result;
  164. }
  165. }
  166. return result;
  167. }
  168. #if NET_2_0
  169. protected virtual
  170. #endif
  171. Type [] DefaultServices {
  172. get {
  173. #if NET_2_0
  174. return new Type [] { typeof (IServiceContainer), typeof (ServiceContainer)};
  175. #else
  176. return new Type [] { typeof (IServiceContainer) };
  177. #endif
  178. }
  179. }
  180. #if NET_2_0
  181. public void Dispose ()
  182. {
  183. this.Dispose (true);
  184. GC.SuppressFinalize (this);
  185. }
  186. protected virtual void Dispose (bool disposing)
  187. {
  188. if (!_disposed) {
  189. if (disposing) {
  190. if (services != null) {
  191. foreach (object obj in services) {
  192. if (obj is IDisposable) {
  193. ((IDisposable) obj).Dispose ();
  194. }
  195. }
  196. services = null;
  197. }
  198. }
  199. _disposed = true;
  200. }
  201. }
  202. #endif
  203. }
  204. }