/// <summary> /// /// </summary> public static void RegisterAllModules(this ContainerBuilder containerBuilder, params string[] searchPatterns) { ReflectionService reflectionService = new ReflectionService(); Log.Trace("Searching for and registering all Autofac Modules"); List <Assembly> assemblies = reflectionService.GetLocalAssemblies(searchPatterns); Log.Trace(m => m( "Found {0} assemblies: {1}" , assemblies.Count , assemblies.ToCsv(", ", a => a.GetName().Name))); List <Type> allTypesFromAllAssemblies = reflectionService.GetAllTypes(assemblies); List <Type> candidateAutofacAssemblyTypes = ( from t in allTypesFromAllAssemblies .Where(type => typeof(Autofac.Module).IsAssignableFrom(type)) .Where(type => type.GetConstructor(Type.EmptyTypes) != null) select t ).ToList(); Log.Trace(m => m("Found {0} candidate AutofacModule types: {1}", candidateAutofacAssemblyTypes.Count, candidateAutofacAssemblyTypes.ToCsv(", ", t => t.FullName))); candidateAutofacAssemblyTypes .Select(Activator.CreateInstance) .Cast <IModule>() .ToList() .ForEach(module => { Log.Trace(m => m("Registering Autofac Module: '{0}'", module.GetType().FullName)); containerBuilder.RegisterModule(module); }); }
public static void RegisterAllOfType <T>(this ContainerBuilder builder) { ReflectionService reflectionService = new ReflectionService(); List <Assembly> assemblies = reflectionService.GetLocalAssemblies(); List <Type> allTypesFromAllAssemblies = reflectionService.GetAllTypes(assemblies); List <Type> typeImplementations = reflectionService.GetImplementationsOf <T>(allTypesFromAllAssemblies); Log.Trace(m => m("Found {0} types implementing {2}: {1}", typeImplementations.Count, typeImplementations.ToCsv(", ", t => t.Name), typeof(T).Name)); typeImplementations.ForEach(t => { Log.Trace(m => m("Registering {0}", t.Name)); builder.RegisterType(t).PropertiesAutowired(); }); }