31 lines
829 B
C#
31 lines
829 B
C#
using System.Reflection;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Tiger.Communication.Messages.Interfaces;
|
|
|
|
namespace Tiger.Utils;
|
|
|
|
public static class DependencyInjectionExtensions
|
|
{
|
|
public static void RegisterOnInherited<T>(this IServiceCollection services)
|
|
{
|
|
var assembly = Assembly.GetExecutingAssembly();
|
|
IEnumerable<Type> 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);
|
|
}
|
|
}
|
|
|
|
} |