2 // System.ComponentModel.Design.ServiceContainer.cs
5 // Martin Willemoes Hansen (mwh@sysrq.dk)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 // Ivan N. Zlatev (contact i-nZ.net)
9 // (C) 2003 Martin Willemoes Hansen
10 // (C) 2003 Andreas Nahr
11 // (C) 2006 Ivan N. Zlatev
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Collections;
37 namespace System.ComponentModel.Design
41 public class ServiceContainer : IServiceContainer, IServiceProvider, IDisposable
43 public sealed class ServiceContainer : IServiceContainer, IServiceProvider
47 private IServiceProvider parentProvider;
48 private Hashtable services = new Hashtable ();
50 private bool _disposed = false;
53 public ServiceContainer()
58 public ServiceContainer (IServiceProvider parentProvider)
60 this.parentProvider = parentProvider;
63 public void AddService (Type serviceType, object serviceInstance)
65 AddService (serviceType, serviceInstance, false);
68 public void AddService (Type serviceType, ServiceCreatorCallback callback)
70 AddService (serviceType, callback, false);
78 void AddService (Type serviceType,
79 object serviceInstance,
82 if (serviceType == null)
83 throw new ArgumentNullException ("serviceType", "Cannot be null");
85 if (parentProvider != null)
86 ((IServiceContainer)parentProvider.GetService(typeof(IServiceContainer))).AddService (serviceType, serviceInstance, promote);
87 if (services.Contains (serviceType)) {
88 throw new ArgumentException (string.Format ("The service {0} already exists in the service container.", serviceType.ToString()));
90 services.Add (serviceType, serviceInstance);
98 void AddService (Type serviceType,
99 ServiceCreatorCallback callback,
102 if (serviceType == null)
103 throw new ArgumentNullException ("serviceType", "Cannot be null");
105 if (parentProvider != null)
106 ((IServiceContainer)parentProvider.GetService(typeof(IServiceContainer))).AddService (serviceType, callback, promote);
107 if (services.Contains (serviceType)) {
108 throw new ArgumentException (string.Format ("The service {0} already exists in the service container.", serviceType.ToString()));
110 services.Add (serviceType, callback);
113 public void RemoveService (Type serviceType)
115 RemoveService (serviceType, false);
119 public virtual void RemoveService (Type serviceType, bool promote)
121 public void RemoveService (Type serviceType, bool promote)
124 if (serviceType == null)
125 throw new ArgumentNullException ("serviceType", "Cannot be null");
127 if (parentProvider != null)
128 ((IServiceContainer)parentProvider.GetService(typeof(IServiceContainer))).RemoveService (serviceType, promote);
130 services.Remove (serviceType);
138 object GetService (Type serviceType)
141 object result = null;
142 Type[] defaultServices = this.DefaultServices;
143 for (int i=0; i < defaultServices.Length; i++) {
144 if (defaultServices[i] == serviceType) {
150 result = services[serviceType];
152 object result = services[serviceType];
154 if (result == null && parentProvider != null)
155 result = parentProvider.GetService (serviceType);
156 if (result != null) {
157 ServiceCreatorCallback cb = result as ServiceCreatorCallback;
159 result = cb (this, serviceType);
160 services[serviceType] = result;
168 protected virtual Type [] DefaultServices {
169 get { return new Type [] { typeof (IServiceContainer), typeof (ServiceContainer)}; }
172 public void Dispose ()
175 GC.SuppressFinalize(this);
178 protected virtual void Dispose (bool disposing)
182 if (this.services != null) {
183 foreach (object obj in this.services) {
184 if (obj is IDisposable) {
185 ((IDisposable) obj).Dispose ();
188 this.services = null;