using System.Reflection; using Microsoft.Extensions.DependencyInjection; using Tiger.Communication.Messages.Interfaces; namespace Tiger.Utils; public static class DependencyInjectionExtensions { public static void RegisterOnInherited(this IServiceCollection services) { var assembly = Assembly.GetExecutingAssembly(); IEnumerable types; if (typeof(T).IsInterface) { types = assembly.GetTypes() .Where(t => t.GetInterfaces().Contains(typeof(T)) && !t.IsAbstract); } else { types = assembly.GetTypes().Where(t => t.BaseType == typeof(T)); } // Register each type with AddSingleton foreach (var type in types) { services.AddSingleton(typeof(T), type); } } }