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
40 public class ServiceContainer : IServiceContainer, IServiceProvider, IDisposable
42 public sealed class ServiceContainer : IServiceContainer, IServiceProvider
45 private IServiceProvider parentProvider;
46 private Hashtable services;
48 private bool _disposed;
51 public ServiceContainer()
56 private Hashtable Services {
59 services = new Hashtable ();
64 public ServiceContainer (IServiceProvider parentProvider)
66 this.parentProvider = parentProvider;
69 public void AddService (Type serviceType, object serviceInstance)
71 AddService (serviceType, serviceInstance, false);
74 public void AddService (Type serviceType, ServiceCreatorCallback callback)
76 AddService (serviceType, callback, false);
84 void AddService (Type serviceType,
85 object serviceInstance,
88 if (promote && parentProvider != null) {
89 IServiceContainer container = (IServiceContainer)
90 parentProvider.GetService (typeof (IServiceContainer));
91 container.AddService (serviceType, serviceInstance, promote);
95 if (serviceType == null)
96 throw new ArgumentNullException ("serviceType");
97 if (serviceInstance == null)
98 throw new ArgumentNullException ("serviceInstance");
99 if (Services.Contains (serviceType))
100 throw new ArgumentException (string.Format (
101 "The service {0} already exists in the service container.",
102 serviceType.ToString ()), "serviceType");
103 Services.Add (serviceType, serviceInstance);
111 void AddService (Type serviceType,
112 ServiceCreatorCallback callback,
115 if (promote && parentProvider != null) {
116 IServiceContainer container = (IServiceContainer)
117 parentProvider.GetService (typeof (IServiceContainer));
118 container.AddService (serviceType, callback, promote);
122 if (serviceType == null)
123 throw new ArgumentNullException ("serviceType");
124 if (callback == null)
125 throw new ArgumentNullException ("callback");
126 if (Services.Contains (serviceType))
127 throw new ArgumentException (string.Format (
128 "The service {0} already exists in the service container.",
129 serviceType.ToString ()), "serviceType");
130 Services.Add (serviceType, callback);
133 public void RemoveService (Type serviceType)
135 RemoveService (serviceType, false);
139 public virtual void RemoveService (Type serviceType, bool promote)
141 public void RemoveService (Type serviceType, bool promote)
144 if (promote && parentProvider != null) {
145 IServiceContainer container = (IServiceContainer)
146 parentProvider.GetService (typeof (IServiceContainer));
147 container.RemoveService (serviceType, promote);
151 if (serviceType == null)
152 throw new ArgumentNullException ("serviceType");
153 Services.Remove (serviceType);
161 object GetService (Type serviceType)
163 object result = null;
165 Type[] defaultServices = this.DefaultServices;
166 for (int i=0; i < defaultServices.Length; i++) {
167 if (defaultServices[i] == serviceType) {
174 result = Services [serviceType];
175 if (result == null && parentProvider != null)
176 result = parentProvider.GetService (serviceType);
177 if (result != null) {
178 ServiceCreatorCallback cb = result as ServiceCreatorCallback;
180 result = cb (this, serviceType);
181 Services [serviceType] = result;
190 Type [] DefaultServices {
193 return new Type [] { typeof (IServiceContainer), typeof (ServiceContainer)};
195 return new Type [] { typeof (IServiceContainer) };
201 public void Dispose ()
204 GC.SuppressFinalize (this);
207 protected virtual void Dispose (bool disposing)
211 if (services != null) {
212 foreach (object obj in services) {
213 if (obj is IDisposable) {
214 ((IDisposable) obj).Dispose ();