Suppose I have a small hierarchy of animal inheritance:
public interface IAnimal {
string Speak();
}
public class Animal : IAnimal {
public Animal() {}
public string Speak() {
return "[Animal] Growl!";
}
}
public class Ape : IAnimal {
public string Speak() {
return "[Ape] Rawrrrrrrr!";
}
}
public class Bat : IAnimal {
public string Speak() {
return "[Bat] Screeeeeee!";
}
}
Next, there is an interface that offers a way to turn stringsinto IAnimals.
public interface ITransmogrifier<T> where T : IAnimal {
T Transmogrify(string s);
}
And finally, here is one strategy for this:
public class Transmogrifier<T> : ITransmogrifier<T> where T : IAnimal, new() {
public T Transmogrify(string s) {
T t = default(T);
if (typeof(T).Name == s)
t = new T();
return t;
}
}
Now, the question. Is it possible to replace the sections marked [1], [2] and [3] with such that this program will compile and work correctly? If you cannot do this without touching parts other than [1], [2] and [3], can you get IAnimalfrom each instance Transmogrifierin the collection containing arbitrary IAnimal implementations? Can you create such a collection to begin with?
static void Main(string[] args) {
var t = new Transmogrifier<Ape>();
Ape a = t.Transmogrify("Ape");
Console.WriteLine(a.Speak()); // Works!
// But can we make an arbitrary collection of such animals?
var list = new List<Transmogrifier< [1] >>() {
// [2]
};
// And how about we call Transmogrify() on each one?
foreach (/* [3] */ transmogrifier in list) {
IAnimal ia = transmogrifier.Transmogrify("Bat");
}
}
}