using Microsoft.Extensions.DependencyInjection; using System.Reflection; namespace DS.Module.Core.Modules; public class DSAppModule : IDSAppModule { public bool Enable { get; set; } = true; private ConfigureServicesContext _configureServicesContext; public virtual void ApplicationInitialization(ApplicationContext context) { } public virtual void ConfigureServices(ConfigureServicesContext context) { } protected internal ConfigureServicesContext ConfigureServicesContext { get { if (_configureServicesContext == null) { //throw new DSAppException($"{nameof(ConfigureServicesContext)}仅适用于{nameof(ConfigureServices)}方法。"); } return _configureServicesContext; } internal set => _configureServicesContext = value; } /// /// 获取模块程序集 /// /// /// public Type[] GetDependedTypes(Type moduleType = null) { if (moduleType == null) { moduleType = GetType(); } var dependedTypes = moduleType.GetCustomAttributes().OfType().ToArray(); if (dependedTypes.Length == 0) { return new Type[0]; } List dependList = new List(); foreach (var dependedType in dependedTypes) { var dependeds = dependedType.GetDependedTypes(); if (dependeds.Length == 0) { continue; } dependList.AddRange(dependeds); foreach (Type type in dependeds) { dependList.AddRange(GetDependedTypes(type)); } } return dependList.Distinct().ToArray(); } /// /// 判断是否是模块 /// /// /// public static bool IsAppModule(Type type) { var typeInfo = type.GetTypeInfo(); return typeInfo.IsClass && !typeInfo.IsAbstract && !typeInfo.IsGenericType && typeof(IDSAppModule).GetTypeInfo().IsAssignableFrom(type); } public void Configure(Action configureOptions) where TOptions : class { ConfigureServicesContext.Services.Configure(configureOptions); } }