using System.Reflection; namespace InServiceQue.Sample; public class HostedServiceRegistrator { public void RegisterHostedService(IServiceCollection services, Type hostedServiceType) { var servicesType = typeof(HostedServiceRegistrator); var methodInfo = servicesType.GetMethod(nameof(AddHostedService)); var genericMethod = methodInfo.MakeGenericMethod(hostedServiceType); genericMethod.Invoke(this, new object[] { services }); } public IServiceCollection AddHostedService(IServiceCollection services) where THostedService : class, IHostedService => services.AddHostedService(); }