private void ParseDirectory(string directory) { ////DirectoryInfo di = new DirectoryInfo(directory); string expandedDirectory = Path.GetFullPath(Environment.ExpandEnvironmentVariables(directory)); string fileName = Path.GetFileName(expandedDirectory); string[] candidateAssemblyPaths = Directory.GetFiles(expandedDirectory, string.Format(CultureInfo.InvariantCulture, @"{0}.dll", fileName), SearchOption.TopDirectoryOnly); if (candidateAssemblyPaths.Length > 0) { AppDomain childAppDomain = this.CreateChildAppDomain(AppDomain.CurrentDomain, expandedDirectory); Type loaderType = typeof(InnerModuleInfoLoader); try { InnerModuleInfoLoader loader = (InnerModuleInfoLoader)childAppDomain.CreateInstanceFrom(loaderType.Assembly.Location, loaderType.FullName).Unwrap(); this.Items.AddRange(loader.LoadModules(candidateAssemblyPaths)); } catch (FileNotFoundException) { // Continue loading assemblies even if an assembly can not be loaded in the new AppDomain } finally { AppDomain.Unload(childAppDomain); } } foreach (string d in Directory.GetDirectories(expandedDirectory)) { ParseDirectory(d); } }
internal ModuleInfo[] GetModuleInfos(string path) { ResolveEventHandler value = (object sender, ResolveEventArgs args) => this.OnReflectionOnlyResolve(args); AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += value; var assembly = AppDomain.CurrentDomain.ReflectionOnlyGetAssemblies().First((Assembly asm) => asm.FullName == typeof(IModule).Assembly.FullName); var type = assembly.GetType(typeof(IModule).FullName); var notAllreadyLoadedModuleInfos = InnerModuleInfoLoader.GetNotAllreadyLoadedModuleInfos(path, type); var result = notAllreadyLoadedModuleInfos.ToArray <ModuleInfo>(); AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve -= value; return(result); }
/// <summary> /// Drives the main logic of building the child domain and searching for the assemblies. /// </summary> protected override void InnerLoad() { if (string.IsNullOrEmpty(this.ModulePath)) { throw new InvalidOperationException("ModulePathCannotBeNullOrEmpty"); } if (!Directory.Exists(this.ModulePath)) { throw new InvalidOperationException(string.Format("Directory: {0} NotFound", new object[] { this.ModulePath })); } var appDomain = this.BuildChildDomain(AppDomain.CurrentDomain); InnerModuleInfoLoader innerModuleInfoLoader = null; try { var list = new List <string>(); var collection = from Assembly assembly in AppDomain.CurrentDomain.GetAssemblies() where !(assembly is AssemblyBuilder) && assembly.GetType().FullName != "System.Reflection.Emit.InternalAssemblyBuilder" && !string.IsNullOrEmpty(assembly.Location) select assembly.Location; list.AddRange(collection); Type typeFromHandle = typeof(InnerModuleInfoLoader); innerModuleInfoLoader = (InnerModuleInfoLoader)appDomain.CreateInstanceFrom(typeFromHandle.Assembly.Location, typeFromHandle.FullName).Unwrap(); innerModuleInfoLoader.LoadAssemblies(list); } catch (Exception e) { AppDomain.Unload(appDomain); throw e; } var modulePaths = MissingAssemblyResolverService.ModuleFiles; try { foreach (var modulePath in modulePaths) { base.Items.AddRange(innerModuleInfoLoader.GetModuleInfos(modulePath)); } } finally { AppDomain.Unload(appDomain); } }
private static IEnumerable <ModuleInfo> GetNotAllreadyLoadedModuleInfos(string path, Type moduleType) { try { Assembly.ReflectionOnlyLoadFrom(path); } catch (BadImageFormatException) { } var r = from t in Assembly.ReflectionOnlyLoadFrom(path).GetExportedTypes().Where(new Func <Type, bool>(moduleType.IsAssignableFrom)) where t != moduleType where !t.IsAbstract select t into type select InnerModuleInfoLoader.CreateModuleInfo(type); return(r); }