Call all instances of ISomething in Ninject

I have an ISomething interface with the Start method. I want to get all implementations of this interface (in several assemblies, main and all referenced) and call the Start method when the application starts. How to do it with Ninject 2.2.0.0.NET 4.0?

Autofile response was here Calling all instances of ISomething in Autofac

+6
ninject ninject-2
source share
2 answers

You can try Ninject.Extensions.Conventions :

var kernel = new StandardKernel(); kernel.Bind(c => c.FromThisAssembly() .SelectAllClasses().InheritedFrom<IFoo>() .BindAllInterfaces()); // and later: kernel.GetAll<IFoo>().ToList().ForEach(foo => foo.DoSmth()); 

The required classes are below:

 public interface IFoo { void DoSmth(); } public class Foo1 : IFoo { public void DoSmth() { Console.Out.WriteLine("Foo1"); } } public class Foo2 : IFoo { public void DoSmth() { Console.Out.WriteLine("Foo2"); } } 
+10
source share

You can use reflection to find all classes that implement interface (s): http://cocaine.co.nz/Home/High-On-Ninject-BLLModule

What do you mean by "core"? - call the Start () method, on which one?

0
source share

All Articles