private static IEnumerable<Assembly> GetAssembliesInFolder(Assembly[] loadedAsms, DirectoryInfo binPath) { List<Assembly> assemblies = new List<Assembly>(); foreach (var fileSystemInfo in binPath.GetFileSystemInfos("*.dll")) { var assemblyName = AssemblyName.GetAssemblyName(fileSystemInfo.FullName); //first try to load assembly from refered libraries instead of plugin 'binaries' folder Assembly assembly = null; if (loadedAsms.Any(x => x.FullName == assemblyName.FullName)) { assembly = Assembly.Load(assemblyName); break; } //if not found in referenced libraries, load from plugin binaries location if (assembly == null) { assembly = Assembly.LoadFile(fileSystemInfo.FullName); assemblies.Add(assembly); } } return assemblies; }
/// <summary> /// In order for Assembly.GetCallingAssembly() to correctly reference this class' consumer, it must /// be invoked from a public method. Otherwise, Assembly.GetCallingAssembly() will reference this /// assembly, which obviously contains no AutoMapper.Profile implementations. /// /// This is a helper method for substituting the consuming assembly when no other assemblies are /// explicitly passed to any of the public RegisterProfiles method overloads. /// </summary> /// <param name="argAssemblies"> /// The assemblies that the consuming assembly explcitly asked us to scan. /// </param> /// <param name="callingAssembly"> /// The consuming assembly. /// </param> /// <returns> /// An enumeration of assemblies to be scanned for AutoMapper.Profile class implementations. /// </returns> private static IEnumerable<Assembly> EnsureAssembly(IEnumerable<Assembly> argAssemblies, Assembly callingAssembly) { // In order for GetCallingAssembly to work, it must be invoked from a public method. // This is why the callingAssembly arg is passed from public methods. // create an initial default empty array of assemblies in case argAssemblies is null var assembliesArray = new Assembly[] { }; // when argAssemblies is not null, enumerate it as an array to prevent multiple enumerations if (argAssemblies != null) assembliesArray = argAssemblies.ToArray(); // when there are no assemblies explicitly defined, return the calling assembly only if (!assembliesArray.Any()) return new[] { callingAssembly }; // otherwise, return the explicitly defined assemblies return assembliesArray; }