Is is possible to do an end-run around generics covariance in C# < 4 in this hypothetical situation?
Posted
by John Feminella
on Stack Overflow
See other posts from Stack Overflow
or by John Feminella
Published on 2010-05-07T17:04:30Z
Indexed on
2010/05/07
17:08 UTC
Read the original article
Hit count: 220
Suppose I have a small inheritance hierarchy of Animals:
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, here's an interface offering a way to turn strings
into IAnimals
.
public interface ITransmogrifier<T> where T : IAnimal {
T Transmogrify(string s);
}
And finally, here's one strategy for doing that:
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] such that this program will compile and run correctly? If you can't do it without touching parts other than [1], [2], and [3], can you still get an IAnimal
out of each instance of a Transmogrifier
in a collection containing arbitrary implementations of an IAnimal? Can you even form 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");
}
}
}
© Stack Overflow or respective owner