/// <summary> /// Makes sure matching assemblies in the supplied folder are loaded in the app domain. /// 加载指定目录匹配的应用程序集 /// </summary> /// <param name="directoryPath"> /// The physical path to a directory containing dlls to load in the app domain. /// </param> public void LoadMatchingAssemblies(string directoryPath) { var loadedAssemblyNames = new List <string>(); foreach (var assembly in GetAssemblies()) { loadedAssemblyNames.Add(assembly.FullName); } if (!FileCommon.IsExistDirectory(directoryPath)) { return; } foreach (var dllPath in FileCommon.GetFiles(directoryPath, "*.dll")) { try { var an = AssemblyName.GetAssemblyName(dllPath); if (Matches(an.FullName) && !loadedAssemblyNames.Contains(an.FullName)) { App.Load(an); } } catch (BadImageFormatException ex) { _logger?.LogError(ex.ExtractAllStackTrace()); } } }
/// <summary> /// 获取指定文件夹下的应用程序集 /// </summary> /// <param name="directoryPaths">指定文件目录集合</param> /// <returns></returns> public IEnumerable <Assembly> GetAssemblies(IEnumerable <string> directoryPaths) { List <Assembly> assemblies = new List <Assembly>(); var loadedAssemblyNames = new List <string>(); TryAddAssembly(assemblies, loadedAssemblyNames, GetAssemblies()); foreach (var directoryPath in directoryPaths) { if (!FileCommon.IsExistDirectory(directoryPath)) { continue; } foreach (var dllPath in FileCommon.GetFiles(directoryPath, "*.dll")) { try { var an = AssemblyName.GetAssemblyName(dllPath); if (Matches(an.FullName) && !loadedAssemblyNames.Contains(an.FullName)) { App.Load(an); assemblies.Add(Assembly.Load(an.FullName)); } } catch (BadImageFormatException ex) { _logger?.LogError(ex.ExtractAllStackTrace()); } } } return(assemblies.Distinct()); }