减少类似的依赖注入定义?

Reduce similar dependency injections definitions?

以下代码将 Handler#<MyClass#> 注入 IHandler<MyClass#>。如何使用反射来减少行数?或者还有其他办法吗?

public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages();
    services.AddServerSideBlazor();
    // ....
    services.AddTransient<IHandler<MyClass1>, AHandler>();
    services.AddTransient<IHandler<MyClass2>, BHandler>();
    services.AddTransient<IHandler<MyClass3>, CHandler>();
    services.AddTransient<IHandler<MyClass4>, DHandler>();
    services.AddTransient<IHandler<MyClass5>, EHandler>();
    services.AddTransient<IHandler<MyClass6>, FHandler>();
    services.AddTransient<IHandler<MyClass7>, GHandler>();
    // .... more
}

如果您需要反射方式,请尝试 Scrutor

services.Scan(scan => scan.FromEntryAssembly()
     .AddClasses(classes => classes.AssignableTo(typeof(IHandler<>)))
     .AsSelf()
     .WithTransientLifetime());