You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.4 KiB
C#

1 year ago
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;
}
/// <summary>
/// 获取模块程序集
/// </summary>
/// <param name="moduleType"></param>
/// <returns></returns>
public Type[] GetDependedTypes(Type moduleType = null)
{
if (moduleType == null)
{
moduleType = GetType();
}
var dependedTypes = moduleType.GetCustomAttributes().OfType<IDependedTypesProvider>().ToArray();
if (dependedTypes.Length == 0)
{
return new Type[0];
}
List<Type> dependList = new List<Type>();
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();
}
/// <summary>
/// 判断是否是模块
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
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<TOptions>(Action<TOptions> configureOptions) where TOptions : class
{
ConfigureServicesContext.Services.Configure<TOptions>(configureOptions);
}
}