当前位置: 动力学知识库 > 问答 > 编程问答 >

.net core dependency injection -> Get all implementations of an interface

问题描述:

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>();
分享给朋友:
您可能感兴趣的文章:
随机阅读: