TigerEmu/Utils/DependencyInjectionExtensio...

25 lines
827 B
C#
Raw 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
{
public static void RegisterMessageEvents(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 messageEventTypes = assembly.GetTypes()
.Where(t => t.GetInterfaces().Contains(typeof(IMessageEvent)) && !t.IsAbstract);
// Register each type with AddSingleton
foreach (var type in messageEventTypes)
{
services.AddSingleton(typeof(IMessageEvent), type);
}
}
}