public bool TryLoad(ModuleDescriptor descriptor, out ModuleAccess access) { //对于 Full Framework : //ModuleDescriptor.Path :(嵌入的模块配置文件)资源名称。 //ModuleDescriptor.Root :程序集文件名。 access = null; if (File.Exists(descriptor.LibraryPath)) { string resourceName = descriptor.ModuleManifest; Assembly assembly = null; try { assembly = _assemblyReader.ReadFile(descriptor.LibraryPath); access = new ModuleAccess(); access.Descriptor = descriptor; access.Assembly = assembly; access.Location = descriptor.LibraryPath; access.ExportedTypes = this.LoadTypes(descriptor.Name, assembly); return(true); } catch (BadImageFormatException) { } } return(false); }
/// <summary> /// 参见 <see cref="EmbededFilesFinder.FindAvailableModules"/> 方法。 /// </summary> public IEnumerable <ModuleDescriptor> FindAvailableModules() { ConcurrentDictionary <String, ModuleDescriptor> modules = new ConcurrentDictionary <string, ModuleDescriptor>(); IEnumerable <string> folders = this.GetFolders().Where(f => Directory.Exists(f)); Parallel.ForEach(folders, (f) => { var files = Directory.EnumerateFiles(f, "*.dll", SearchOption.TopDirectoryOnly) .Where(file => { string name = Path.GetFileName(file); return (!name.StartsWith("system.", StringComparison.OrdinalIgnoreCase) && !name.StartsWith("microsoft.", StringComparison.OrdinalIgnoreCase) && !name.StartsWith("schubert.framework", StringComparison.OrdinalIgnoreCase)); }).ToArray(); foreach (var file in files) { Assembly assembly = null; try { assembly = _reader.ReadFile(file); } catch (BadImageFormatException) { continue; } var resourceNames = assembly.GetManifestResourceNames(); foreach (string r in resourceNames) { string resourceFile = GetResourceFileName(assembly, r); foreach (IModuleHarvester harverter in _moduleHarvesters) { if (harverter.CanHarvest(resourceFile)) { _logger.WriteDebug($"尝试加载模块嵌入式清单文件 {file} , 资源路径:{r}。"); using (Stream stream = assembly.GetManifestResourceStream(r)) { string content = stream.ReadToEnd(System.Text.Encoding.UTF8); ModuleDescriptor desc = null; if (harverter.TryHarvestModule(content, out desc)) { desc.RootDirectory = f; desc.LibraryPath = file; desc.ModuleManifest = GetResourceFileName(assembly, r, desc.RootNamespce); modules.GetOrAdd(desc.Name, desc); } } } } } } }); return(modules.Values.ToArray()); }
public bool TryLoad(ModuleDescriptor descriptor, out ModuleAccess access) { access = null; Assembly assembly = null; string libraryPath = descriptor.LibraryPath; if (File.Exists(libraryPath)) { try { assembly = _assemblyReader.ReadFile(libraryPath); } catch (BadImageFormatException) { this._logger.WriteWarning($"{libraryPath} 不是有效的程序集文件。"); } } else { try { assembly = _assemblyReader.ReadByName(descriptor.LibraryPath); } catch (Exception ex) { ex.ThrowIfNecessary(); } } if (assembly != null) { access = new ModuleAccess(); access.Descriptor = descriptor; access.Assembly = assembly; access.Location = descriptor.LibraryPath; access.ExportedTypes = this.LoadTypes(descriptor.ModuleManifest, assembly, descriptor.RootNamespce.IfNullOrWhiteSpace(assembly.GetName().Name)); return(true); } return(false); }