ServiceContainer.cs 5.7 KB

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