'IAnimal Interface implemnted by Dog and Cat and DogViewmodel and catViewModel takes IAnimal dependency. How to inject correct obj cat to CatViewmodel

I was asked the below problem in interview of injecting the correct type in dependency injection. I told i will use factory pattern and check the type before injecting the object. Then interviewer asked me if there are 100 classes will you do for all the classes. then he gave hint to use Ienumerable. But I did not get the solution. Below explained the problem statements. Please help me to understand the solution for the below problem.

in WPF MVVM pattern Model : Has an Interface called IAnimal and two classes are implementing it.

public class Dog : IAnimal
{
}
public class Cat : IAnimal
{
}

in viewmodel having two classes CatViewModel and DogViewModel

public class CatViewModel
{
    public CatViewModel(IAnimal animalObj)
    {
    }
}

public class DogViewModel
{
    public DogViewModel(IAnimal animalObj)
    {
    }
}

here DogViewModel should take only DogModel object and CatViewModel should take only CatModel Object. Here constructor is having IAnimal type so there can be posibility of injecting wrong object. How to protect and inject correct type.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source