Fixing an issue with output parameters that are of type IntPtr
[castle.git] / Facilities / DynamicLoader / Castle.Facilities.DynamicLoader / DynamicLoaderRegistry.cs
blob7626720a677e6bbb43af125832d73c12c113cc32
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Facilities.DynamicLoader
17 using System;
18 using System.Collections.Generic;
20 using Castle.MicroKernel.Facilities;
22 /// <summary>
23 /// Stores instances of <see cref="RemoteLoader"/>.
24 /// </summary>
25 public class DynamicLoaderRegistry : IDisposable
27 private Dictionary<string, RemoteLoader> loaders = new Dictionary<string, RemoteLoader>();
29 /// <summary>
30 /// Register a new loader, for the specified <paramref name="domainId"/>.
31 /// </summary>
32 public void RegisterLoader(string domainId, RemoteLoader loader)
34 if (loaders == null)
35 throw new ObjectDisposedException("DynamicLoaderRegistry");
37 loaders.Add(domainId, loader);
40 /// <summary>
41 /// Gets the <see cref="RemoteLoader"/> instance for the specified <paramref name="domainId"/>.
42 /// </summary>
43 public RemoteLoader GetLoader(string domainId)
45 if (loaders == null)
46 throw new ObjectDisposedException("DynamicLoaderRegistry");
48 RemoteLoader loader;
49 if (!loaders.TryGetValue(domainId, out loader))
50 throw new FacilityException("Domain not found: " + domainId);
52 return loader;
55 /// <summary>
56 /// Registers a specific component on a specific domain.
57 /// </summary>
58 /// <remarks>
59 /// The implementation simply calls <see cref="GetLoader"/> to get the correct
60 /// <see cref="RemoteLoader"/>, then add the component to the <see cref="RemoteLoader.Kernel"/>.
61 /// </remarks>
62 public void RegisterComponentOnDomain(string domainId, string key, Type service, Type component)
64 if (loaders == null)
65 throw new ObjectDisposedException("DynamicLoaderRegistry");
67 GetLoader(domainId).Kernel.AddComponent(key, service, component);
70 /// <summary>
71 /// Implementation of <see cref="IDisposable"/>.
72 /// </summary>
73 public void Dispose()
75 foreach (RemoteLoader loader in loaders.Values)
77 loader.Kernel.Parent.RemoveChildKernel(loader.Kernel);
79 loader.Dispose();
81 AppDomain.Unload(loader.AppDomain);
84 loaders.Clear();
85 loaders = null;