Paul
Paul

Reputation: 93

Calling all ISomething instances in Ninject

I have an interface ISomething with a method Start. I want to get all implementations of this interface (in multiple assemblies, the main one and all referenced ones) and call the Start method on application start. How can I do this with Ninject 2.2.0.0 .NET 4.0?

Autofac answer was here Calling all ISomething instances in Autofac

Upvotes: 6

Views: 1553

Answers (2)

the_joric
the_joric

Reputation: 12261

You may 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());

Needed 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");
    }
}

Upvotes: 10

mkaj
mkaj

Reputation: 3489

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

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

Upvotes: 0

Related Questions