Created
July 30, 2010 18:27
-
-
Save nesizer/501066 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class AutofacScanningExtensions | |
{ | |
public static IRegistrationBuilder<object, ScanningActivatorData, DynamicRegistrationStyle> | |
RegisterAssemblyTypesFromPath(this ContainerBuilder builder, string path) | |
{ | |
return RegisterAssemblyTypesFromPath(builder, path, null, a => true); | |
} | |
public static IRegistrationBuilder<object, ScanningActivatorData, DynamicRegistrationStyle> | |
RegisterAssemblyTypesFromPath(this ContainerBuilder builder, string path, Predicate<Assembly> Assemblyfilter) | |
{ | |
return RegisterAssemblyTypesFromPath(builder, path, null, Assemblyfilter); | |
} | |
public static Autofac.Builder.IRegistrationBuilder<object, ScanningActivatorData, DynamicRegistrationStyle> | |
RegisterAssemblyTypesFromPath(this ContainerBuilder builder, string path, string fileFilter) | |
{ | |
return RegisterAssemblyTypesFromPath(builder, path, fileFilter, a => true); | |
} | |
public static IRegistrationBuilder<object, ScanningActivatorData, DynamicRegistrationStyle> | |
RegisterAssemblyTypesFromPath(this ContainerBuilder builder, string path, string fileFilter, Predicate<Assembly> Assemblyfilter) | |
{ | |
fileFilter = string.IsNullOrEmpty(fileFilter) ? "*" : fileFilter; | |
var filenames = System.IO.Directory.GetFiles(path, fileFilter).Where(IsAssamblyFile); | |
var assemblyNames = FindAssemblies(filenames, Assemblyfilter); | |
var assemblies = assemblyNames.Select(name => Assembly.Load(name)); | |
return builder.RegisterAssemblyTypes(assemblies.ToArray()); | |
} | |
private static AppDomain CreateTempDomain() | |
{ | |
return AppDomain.CreateDomain("Autofac.ScanningDomain", | |
AppDomain.CurrentDomain.Evidence, | |
AppDomain.CurrentDomain.SetupInformation); | |
} | |
private static IEnumerable<AssemblyName> FindAssemblies(IEnumerable<string> filenames, Predicate<Assembly> filter) | |
{ | |
AppDomain TempDomain = CreateTempDomain(); | |
foreach (string file in filenames) | |
{ | |
Assembly assembly; | |
try | |
{ | |
var name = new AssemblyName { CodeBase = file }; | |
assembly = TempDomain.Load(name); | |
} | |
catch (BadImageFormatException) | |
{ | |
continue; | |
} | |
if (filter(assembly)) | |
yield return assembly.GetName(); | |
} | |
AppDomain.Unload(TempDomain); | |
} | |
public const StringComparison comparison = StringComparison.OrdinalIgnoreCase; | |
public static bool IsAssamblyFile(string file) | |
{ | |
string extension = System.IO.Path.GetExtension(file); | |
return string.Equals(extension, ".dll", comparison) || string.Equals(extension, ".exe", comparison); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment