Is there a way of looping through the multiple implementations of an interface? I haven't written any DI code examples as I didn't want to limit suggestions based on a DI framework. Could be Autofac, Ninject, Unity etc. Whatever is suitable for the task.
I'll be using the ASP.Net Core but I believe the built in DI doesn't allow for multiple implementations.
So, a singular Interface.
public interface InterfaceA
{
void MethodA();
}
Numerous classes that implement said interface and are registered.
public class Class1 : InterfaceA
{
public void MethodA()
{
//Do something
}
}
public class Class2 : InterfaceA
{
public void MethodA()
{
//Do something
}
}
Controller such as this
public TestContoller: Controller
{
private readonly List<InterfaceA> interfaces;
void TestController(List<InterfaceA> interfaces)
{
this.interfaces = interfaces;
}
IActionResult TestMethod()
{
Foreach(var implementation in interfaces)
{
implementation.MethodA();
}
return View();
}
}