Search code examples
genericscastle-windsor

castle windsor and generic types when loading from assembly


Im trying to load all types from a specific folder but the base Interface is generic: IAnalysis

the following syntax works for non generic types. :

container.Register(Classes.FromAssemblyInDirectory(new AssemblyFilter(folder)).BasedOn<IAnalysis>());

How can I make it work with my generic type? I need something like this .BasedOn<IAnalysis<T>>

Thanks

Amit


Solution

  • You can do it like this :

    public class MyModuleInstaller : IWindsorInstaller
    {
        public static bool IsContructable(this Type t)
        {
            return !t.IsAbstract && !t.ContainsGenericParameters;
        }
    
        void IWindsorInstaller.Install(IWindsorContainer container)
        {
            IEnumerable<IRegistration> myRegistrations = GetRegistrations();
            if (myRegistrations != null)
            {
                container.Register(myRegistrations .ToArray());
            }
        }
    
        protected virtual IEnumerable<IRegistration> GetRegistrations()
        {
            return new IRegistration[]
            {
                Classes.FromAssembly(GetAssemblyNamed("TheNameOfMyAssembly"))
                    .BasedOn<IMyInterfaceOrMyGenericBase>()
                    .If(x => Component.IsInNamespace("MyNamespace", true)(x)
                             && x.IsContructable())
                    .WithServices(new[] {typeof (IMyInterfaceOrMyGenericBase) }))
                };
        }
    
        // you can remove this method. Added for your convenience
        protected virtual Assembly GetAssemblyNamed(string assemblyName)
        {
            try
            {
                if(Assembly.GetExecutingAssembly().FullName.Split(',')[0] == assemblyName)
                {
                    return Assembly.GetExecutingAssembly();
                }
    
                Assembly fromDynamicModuleLoader = 
                    MyModuleLoader.MyAssemblies.FirstOrDefault(
                        x => x.GetName().Name == assemblyName);
                if(fromDynamicModuleLoader != null)
                {
                    return fromDynamicModuleLoader;
                }
    
                if(_loadedInAppDomain == null)
                {
                    _loadedInAppDomain =
                        System.AppDomain.CurrentDomain.GetAssemblies()
                            .Where(x => x.FullName.Contains(assemblyName))
                            .ToArray();
                }
    
                var fromAppDomain = 
                    _loadedInAppDomain.FirstOrDefault(
                        x => x.FullName.Split(',')[0] == assemblyName);
    
                if(fromAppDomain != null)
                {
                    return fromAppDomain;
                }
    
                return ReflectionUtil.GetAssemblyNamed(assemblyName);
            }
            catch(FileNotFoundException)
            {
                throw;
            }
        }
    }