TigerEmu/Utils/DependencyInjectionExtensio...

25 lines
780 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)
{
// Get the assembly you are interested in (this could be any Assembly)
var assembly = Assembly.GetExecutingAssembly();
// Get all types that implement IMessageEvent and are not abstract
var types = assembly.GetTypes()
.Where(t => t.GetInterfaces().Contains(typeof(T)) && !t.IsAbstract);
// Register each type with AddSingleton
foreach (var type in types)
{
services.AddSingleton(typeof(T), type);
}
}
}