using System; using System.Linq; using System.Reflection; using Microsoft.Extensions.DependencyInjection; using Prism.Ioc; using RIZO_Application.Infrastructure.CustomAttribute; namespace RIZO_Application.Core.Infrastructure.CustomExtensions { public static class ContainerRegistryExtensions { public static void AutoRegisterServices(this IContainerRegistry containerRegistry, Assembly assembly) { var types = assembly.GetTypes() .Where(t => t.IsClass && !t.IsAbstract && t.GetCustomAttribute() != null); foreach (var type in types) { var attribute = type.GetCustomAttribute(); var serviceType = attribute.ServiceType ?? type; // 默认注册为自身 switch (attribute.Lifetime) { case RIZO_Application.Infrastructure.CustomAttribute.ServiceLifetime.Singleton: containerRegistry.RegisterSingleton(serviceType, type); break; case RIZO_Application.Infrastructure.CustomAttribute.ServiceLifetime.Scoped: containerRegistry.RegisterScoped(serviceType, type); break; default: // Transient containerRegistry.Register(serviceType, type); break; } } } public static void AutoRegisterViews(this IContainerRegistry containerRegistry, Assembly assembly) { var viewTypes = assembly.GetTypes() .Where(t => t.IsClass && !t.IsAbstract && t.GetCustomAttribute() != null); foreach (var viewType in viewTypes) { var attribute = viewType.GetCustomAttribute(); string viewName = attribute?.ViewName ?? viewType.Name; // 默认使用类名 containerRegistry.RegisterForNavigation(viewType, viewName); } } } }