问题描述:

I have a interface called IRule and multiple cs classes that implement this interface. I want to uses the .net core dependency injection to load all implementation of IRule, so all implemented rules.

Unfortunately I can't make this work. I know I can inject an IEnumerable into my ctor of the controller, but I don't know how to register this setup in the Startup.cs

Hope you can help me with this!

Cheers,

Nik

网友答案:

To my knowledge, it's just a matter of registering all IRule implementations one by one; the MS.Ext.DI library can resolve it as an IEnumerable<T>.

services.AddTransient<IRule, Rule1>();
services.AddTransient<IRule, Rule2>();
services.AddTransient<IRule, Rule3>();
services.AddTransient<IRule, Rule4>();
相关阅读:
Top