TigerEmu/Utils/DependencyInjectionExtensio...

25 lines
780 B
C#
Raw Permalink Normal View History

2023-09-23 11:11:07 +00:00
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Tiger.Communication.Messages.Interfaces;
namespace Tiger.Utils;
public static class DependencyInjectionExtensions
{
2023-10-14 20:32:12 +00:00
public static void RegisterOnInherited<T>(this IServiceCollection services)
2023-09-23 11:11:07 +00:00
{
// 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
2023-10-14 20:32:12 +00:00
var types = assembly.GetTypes()
.Where(t => t.GetInterfaces().Contains(typeof(T)) && !t.IsAbstract);
2023-09-23 11:11:07 +00:00
// Register each type with AddSingleton
2023-10-14 20:32:12 +00:00
foreach (var type in types)
2023-09-23 11:11:07 +00:00
{
2023-10-14 20:32:12 +00:00
services.AddSingleton(typeof(T), type);
2023-09-23 11:11:07 +00:00
}
}
}