using System.Reflection; namespace InServiceQue.Sample; public class HostedServiceRegistrator { public void RegisterHostedService(IServiceCollection services, Type hostedServiceType) { Type servicesType = typeof(HostedServiceRegistrator); MethodInfo methodInfo = servicesType.GetMethod("AddHostedService"); MethodInfo genericMethod = methodInfo.MakeGenericMethod(hostedServiceType); genericMethod.Invoke(this, new object[] { services }); } // Needed as a work-arround because we can't call the extension method with reflection. public IServiceCollection AddHostedService(IServiceCollection services) where THostedService : class, IHostedService => services.AddHostedService(); }