Is it possible in this hypothetical situation to perform covariance of generics in C # <4?

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");
        }
    }
}
+3
2

, Transmorgifier<Ape> Transmorgifier<Bat> , ( List<object>). , ITransmorgifier :

public interface ITransmogrifier
{
    IAnimal Transmogrify(string s);
}

var list = new List<ITransmogrifier>() {
    new Transmorgifier<Ape>(), new Transmorgifier<Bat>() ...
};
+3

.

, # 4, ITransmogrifier ( "out" ) T. List<ITransmogrifier<IAnimal>> a Transmogrifier<Bat> .

+4

Source: https://habr.com/ru/post/1744535/


All Articles