lundi 16 novembre 2020

How to find and register dependencies using reflection

I want to do the following:

Imagine I have the classes that implement the interface IValidator. I want to find all of those classes and register them in dotnet core standard dependency injection container.

Example:

        services.AddSingleton<IValidator<Delete.Command>, Delete.CommandValidator>();
        services.AddSingleton<IValidator<CreateOrUpdate.Command>, CreateOrUpdate.CommandValidator>();

I know how to find the classes.

var type = typeof(IMyInterface);
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p));

The problem is that I don't know how to register them after I find them.

I don't know what keywords to use to search in internet for information. That's why I decided to ask question here.

Thanks!





Aucun commentaire:

Enregistrer un commentaire