This is my Scenario:
public class Foo
{
private readonly List<Lazy<IAnimal>> _animals;
public Foo(List<Lazy<IAnimal>> animals )
{
_animals = animals;
}
public void Bark()
{
//Line: *
var dog = _animals.First(p => p.GetType() == typeof (Dog)).Value;
}
Public void Mio()
{
//Line: *
var dog = _animals.First(p => p.GetType() == typeof (Cat)).Value;
}
}
public class Dog:IAnimal
{
}
public class Cat:IAnimal
{
}
public interface IAnimal
{
}
Questions:
Here The list of Animals are Lazily injected into the class Foo.
I want to do something like Line * with Ninject. As you may know the problem is that before resolving a class, GetType() returns Null. So how can I resolve just one of the Items in the list whenever I want?
Can Ninject do such a thing at all or I have to change my DI Container?
You can use the OfType extension method provided in the System.Linq namespace.