은 가정하자 나는 동물의 작은 상속 계층 구조가 있습니다이 가상 상황에서 C# <4의 제네릭 공분산을 끝내기가 가능합니까?
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!";
}
}
다음, 여기 IAnimals
에 strings
을 설정하는 방법을 제공하는 인터페이스입니다.
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;
}
}
이제 질문 :
public interface ITransmogrifier<T> where T : IAnimal {
T Transmogrify(string s);
}
는 그리고 마지막으로, 여기에 작업을 수행하기위한 하나 개의 전략이다. [1], [2] 및 [3]으로 표시된 섹션을 바꿀 수 있습니까? 그러면이 프로그램이 올바르게 컴파일되고 실행됩니다. [1], [2], [3] 이외의 부분을 건드릴 수 없다면 Transmogrifier
의 각 인스턴스에서 여전히 IAnimal
을 가져올 수 있으며 IAnimal의 임의의 구현을 포함하는 컬렉션에 있습니까? 그런 컬렉션을 시작할 수 있습니까? Transmorgifier<Ape>
와 Transmorgifier<Bat>
사이에 형의 관계가 그래서 당신은 (List<object>
제외) 같은 일반적인 목록에 넣을 수 없기 때문에
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");
}
}
}
나는 그다지 의심 스럽습니다. 나는 .Cast'으로 할 수있는 몇 가지 속임수가 있기를 바랐지만, 나는 그렇지 않을 것이라고 생각한다. –