/// <summary> /// 设置DI特性的模型依赖 /// </summary> private void SetDependencies() { foreach (var moduleInfo in _modules) { foreach (var referencedAssemblyName in moduleInfo.Assembly.GetReferencedAssemblies()) { var referencedAssembly = Assembly.Load(referencedAssemblyName); var dependedModuleList = _modules.Where(m => m.Assembly == referencedAssembly).ToList(); if (dependedModuleList.Count > 0) { moduleInfo.Dependencies.AddRange(dependedModuleList); } } foreach (var dependedModuleType in LindModule.FindDependedModuleTypes(moduleInfo.Type)) { var dependedModuleInfo = _modules.FirstOrDefault(m => m.Type == dependedModuleType); if (dependedModuleInfo == null) { throw new ArgumentException("Could not find a depended module " + dependedModuleType.AssemblyQualifiedName + " for " + moduleInfo.Type.AssemblyQualifiedName); } if ((moduleInfo.Dependencies.FirstOrDefault(dm => dm.Type == dependedModuleType) == null)) { moduleInfo.Dependencies.Add(dependedModuleInfo); } } } }
/// <summary> /// 递归:添加本类型里DI特性的依赖类型 /// </summary> /// <param name="module"></param> /// <param name="allModules"></param> private static void FillDependedModules(Type module, ICollection <Type> allModules) { foreach (var dependedModule in LindModule.FindDependedModuleTypes(module)) { if (!allModules.Contains(dependedModule)) { allModules.Add(dependedModule); FillDependedModules(dependedModule, allModules); } } }