0

I have few classes(Dog, Cat, Cow...) which implement interface IAnimal. They are putted in World.Animals library. And in another class library let's say World.Earth i want to create instance for each IAnimal. I figured out that GetExecutingAssembly won't work. So what should have looked my code

var types = Assembly. // what's next? GetReferencedAssembly?
foreach(var type in types)
{
var animal = (IAnimal)Activator.CreateInstance(typeof(type));
}
syyyl
  • 31
  • 3
  • Possible duplicate of [Purpose of Activator.CreateInstance with example?](https://stackoverflow.com/questions/7598088/purpose-of-activator-createinstance-with-example) – Daniel B Jul 11 '18 at 15:44
  • look at this to get the types https://stackoverflow.com/questions/26733/getting-all-types-that-implement-an-interface then to instanciate them i guess you will need to typeof the types `(IAnimal)Activator.CreateInstance(typeof(type))` – TiGreX Jul 11 '18 at 15:47
  • TiGreX You are right, my mistake but I still don't know how to include all classes from another library – syyyl Jul 11 '18 at 15:54
  • 1
    You can do something like `typeof(Dog).Assembly` to get the assembly that dog is defined in... – Chris Jul 11 '18 at 15:56

0 Answers0