<PackageReference Include="Castle.Windsor" Version="4.1.0" />

DefaultComponentInstaller

Default IComponentsInstaller implementation.
using Castle.Core; using Castle.Core.Configuration; using Castle.Core.Internal; using Castle.Core.Resource; using Castle.MicroKernel; using Castle.MicroKernel.Registration; using Castle.MicroKernel.SubSystems.Configuration; using Castle.MicroKernel.SubSystems.Conversion; using Castle.Windsor.Configuration.Interpreters; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace Castle.Windsor.Installer { public class DefaultComponentInstaller : IComponentsInstaller { private string assemblyName; public void SetUp(IWindsorContainer container, IConfigurationStore store) { IConversionManager converter = container.Kernel.GetSubSystem(SubSystemConstants.ConversionManagerKey) as IConversionManager; SetUpInstallers(store.GetInstallers(), container, converter); SetUpFacilities(store.GetFacilities(), container, converter); SetUpComponents(store.GetComponents(), container, converter); SetUpChildContainers(store.GetConfigurationForChildContainers(), container); } protected virtual void SetUpInstallers(IConfiguration[] installers, IWindsorContainer container, IConversionManager converter) { Dictionary<Type, IWindsorInstaller> dictionary = new Dictionary<Type, IWindsorInstaller>(); HashSet<Assembly> assemblies = new HashSet<Assembly>(); foreach (IConfiguration installer in installers) { AddInstaller(installer, dictionary, converter, assemblies); } if (dictionary.Count != 0) container.Install(dictionary.Values.ToArray()); } private void AddInstaller(IConfiguration installer, Dictionary<Type, IWindsorInstaller> cache, IConversionManager conversionManager, ICollection<Assembly> assemblies) { string value = installer.Attributes["type"]; if (!string.IsNullOrEmpty(value)) { Type type = conversionManager.PerformConversion<Type>(value); AddInstaller(cache, type); } else { assemblyName = installer.Attributes["assembly"]; if (!string.IsNullOrEmpty(assemblyName)) { Assembly assemblyNamed = ReflectionUtil.GetAssemblyNamed(assemblyName); if (!assemblies.Contains(assemblyNamed)) { assemblies.Add(assemblyNamed); GetAssemblyInstallers(cache, assemblyNamed); } } else { string directoryName = installer.Attributes["directory"]; string mask = installer.Attributes["fileMask"]; string text = installer.Attributes["publicKeyToken"]; AssemblyFilter assemblyFilter = new AssemblyFilter(directoryName, mask); if (text != null) assemblyFilter.WithKeyToken(text); foreach (Assembly assembly in ReflectionUtil.GetAssemblies(assemblyFilter)) { if (!assemblies.Contains(assembly)) { assemblies.Add(assembly); GetAssemblyInstallers(cache, assembly); } } } } } private void GetAssemblyInstallers(Dictionary<Type, IWindsorInstaller> cache, Assembly assembly) { Type[] availableTypes = assembly.GetAvailableTypes(false); foreach (Type item in InstallerTypes(availableTypes)) { AddInstaller(cache, item); } } private IEnumerable<Type> InstallerTypes(IEnumerable<Type> types) { return types.Where(IsInstaller); } private bool IsInstaller(Type type) { if (type.GetTypeInfo().get_IsClass() && !type.GetTypeInfo().get_IsAbstract() && !type.GetTypeInfo().get_IsGenericTypeDefinition()) return type.Is<IWindsorInstaller>(); return false; } private void AddInstaller(Dictionary<Type, IWindsorInstaller> cache, Type type) { if (!cache.ContainsKey(type)) { IWindsorInstaller value = type.CreateInstance<IWindsorInstaller>(Array.Empty<object>()); cache.Add(type, value); } } protected virtual void SetUpFacilities(IConfiguration[] configurations, IWindsorContainer container, IConversionManager converter) { foreach (IConfiguration configuration in configurations) { IFacility facility = converter.PerformConversion<Type>(configuration.Attributes["type"]).CreateInstance<IFacility>(Array.Empty<object>()); container.AddFacility(facility); } } private void AssertImplementsService(IConfiguration id, Type service, Type implementation) { if ((object)service != null) { if (service.GetTypeInfo().get_IsGenericTypeDefinition()) implementation = implementation.MakeGenericType(TypeExtensions.GetGenericArguments(service)); if (!TypeExtensions.IsAssignableFrom(service, implementation)) throw new ComponentRegistrationException(string.Format("Could not set up component '{0}'. Type '{1}' does not implement service '{2}'", id.Attributes["id"], implementation.AssemblyQualifiedName, service.AssemblyQualifiedName)); } } protected virtual void SetUpComponents(IConfiguration[] configurations, IWindsorContainer container, IConversionManager converter) { foreach (IConfiguration configuration in configurations) { Type type = GetType(converter, configuration.Attributes["type"]); Type type2 = GetType(converter, configuration.Attributes["service"]); HashSet<Type> services = new HashSet<Type>(); if ((object)type2 != null) services.Add(type2); CollectAdditionalServices(configuration, converter, services); string name = null; if ((object)type != null) { AssertImplementsService(configuration, type2, type); CastleComponentAttribute defaultsFor = CastleComponentAttribute.GetDefaultsFor(type); if (defaultsFor.ServicesSpecifiedExplicitly && services.Count == 0) defaultsFor.Services.ForEach(delegate(Type s) { services.Add(s); }); name = GetName(defaultsFor, configuration); } if (services.Count != 0 || (object)type != null) container.Register(Component.For(services).ImplementedBy(type).Named(name)); } } private static string GetName(CastleComponentAttribute defaults, IConfiguration component) { if (component.Attributes["id-automatic"] != bool.TrueString) return component.Attributes["id"]; return defaults.Name; } private Type GetType(IConversionManager converter, string typeName) { if (typeName == null) return null; return converter.PerformConversion<Type>(typeName); } private void CollectAdditionalServices(IConfiguration component, IConversionManager converter, ICollection<Type> services) { IConfiguration configuration = component.Children["forwardedTypes"]; if (configuration != null) { foreach (IConfiguration child in configuration.Children) { string value = child.Attributes["service"]; try { services.Add(converter.PerformConversion<Type>(value)); } catch (ConverterException innerException) { throw new ComponentRegistrationException(string.Format("Component {0} defines invalid forwarded type.", component.Attributes["id"]), innerException); } } } } private static void SetUpChildContainers(IConfiguration[] configurations, IWindsorContainer parentContainer) { foreach (IConfiguration configuration in configurations) { new WindsorContainer(configuration.Attributes["name"], parentContainer, new XmlInterpreter(new StaticContentResource(configuration.Value))); } } } }