public AssemblyModuleLoader(Assembly assembly) { if (assembly == null) { throw new ArgumentNullException(nameof(assembly)); } m_loadedModules = new Dictionary <string, Type>(); m_serviceCollection = new ServiceCollection(); foreach (var moduleType in BaristaModuleTypeLoader.LoadBaristaModulesFromAssembly(assembly)) { var baristaModuleAttribute = BaristaModuleAttribute.GetBaristaModuleAttributeFromType(moduleType); if (baristaModuleAttribute.IsDiscoverable == false) { continue; } string targetModuleName = baristaModuleAttribute.Name; if (string.IsNullOrWhiteSpace(targetModuleName)) { throw new InvalidOperationException($"The specfied module ({moduleType}) must indicate a name."); } m_serviceCollection.AddTransient(typeof(IBaristaModule), moduleType); if (m_loadedModules.ContainsKey(targetModuleName)) { throw new InvalidOperationException($"A module with the specified name ({targetModuleName}) has already been registered. Ensure all modules in the specified assembly have unique names."); } m_loadedModules.Add(targetModuleName, moduleType); } }
/// <summary> /// Scans the configured folder for assemblies. /// </summary> private void ScanModuleFolder() { var moduleFolderPathInfo = new DirectoryInfo(m_moduleFolderFullPath); if (!moduleFolderPathInfo.Exists) { return; } //Add our custom assembly resolver AppDomain.CurrentDomain.AssemblyResolve += ModuleAssemblyLoader; try { foreach (var assemblyFileInfo in moduleFolderPathInfo.GetFileSystemInfos("*.dll")) { foreach (var moduleType in BaristaModuleTypeLoader.LoadBaristaModulesFromAssembly(assemblyFileInfo.FullName)) { var baristaModuleAttribute = BaristaModuleAttribute.GetBaristaModuleAttributeFromType(moduleType); if (baristaModuleAttribute.IsDiscoverable == false) { continue; } string targetModuleName = baristaModuleAttribute.Name; if (string.IsNullOrWhiteSpace(targetModuleName)) { throw new InvalidOperationException($"The specfied module ({moduleType}) must indicate a name."); } m_serviceCollection.AddTransient(typeof(IBaristaModule), moduleType); if (m_loadedModules.ContainsKey(targetModuleName)) { throw new InvalidOperationException($"A module with the specified name ({targetModuleName}) has already been registered. ({m_loadedModules[targetModuleName]})"); } m_loadedModules.Add(targetModuleName, moduleType); } } } finally { AppDomain.CurrentDomain.AssemblyResolve -= ModuleAssemblyLoader; } }
public static string GetModuleName(this IBaristaModule module) { string name; switch (module) { case BaristaScriptModule scriptModule: name = scriptModule.Name; break; case BaristaResourceScriptModule resourceScriptModule: name = resourceScriptModule.Name; break; default: var baristaModuleAttribute = BaristaModuleAttribute.GetBaristaModuleAttributeFromType(module.GetType()); name = baristaModuleAttribute.Name; break; } return(name); }