转换集合中使用的泛型?

问题描述

假设我想为宠物主人创建饮食计划以保持宠物健康,并且有以下课程:

// Pets
public class Pet { ... }
public class Dog : Pet { ... }
public class Cat : Pet { ... }

// Meals and diets
public class Meal<T> : ICollection<T> where T : Pet
{
    ...
    private List<T> allowedPets { get; set; } = new List<T>();
    ...
}
public class Diet<T> : ICollection<Meal<T>> where T : Pet
{
    ...
    private List<Meal<T>> dietMeals { get; set; } = new List<Meal<T>>();
    ...
}

虽然我可以列出可以遵循某种饮食的宠物清单,而不是允许某些饮食进食的宠物清单,但这只是一个例子。无论哪种方式,我都可以像这样任意饮食,以某种方式使用它们。

// Dogs and their diets
Dog labrador = new Dog() { ... };
Dog dalmatian = new Dog() { ... };
Meal<Dog> mediumDogBreakfast = new Meal<Dog>(...) { labrador,dalmatian };
Meal<Dog> mediumDoglunch = new Meal<Dog>(...) { labrador,dalmatian };
Meal<Dog> mediumDogDinner = new Meal<Dog>(...) { labrador,dalmatian };
Diet<Dog> mediumDogDiet = new Diet<Dog>()
{
    mediumDogBreakfast,mediumDoglunch,mediumDogDinner
};

// Cats and their diets
Cat siamese = new Cat() { ... };
Cat sphynx = new Cat() { ... };
Meal<Cat> orientalCatBreakfast = new Meal<Cat>(...) { siamese,sphynx };
Meal<Cat> orientalCatLunch = new Meal<Cat>(...) { siamese,sphynx };
Meal<Cat> orientalCatDinner = new Meal<Cat>(...) { siamese,sphynx };
Diet<Cat> orientalCatDiet = new Diet<Cat>()
{
    orientalCatBreakfast,orientalCatLunch,orientalCatDinner
};

但是如果我随后要将这些饮食放在一个列表中,则会收到转换错误

// This isn't allowed!
List<Diet<Pet>> = new List<Diet<Pet>>()
{
    mediumDogDiet,orientalCatDiet
};

我的印象是该列表将允许CatDog对象,因为Pet Costraint更为笼统,但显然这里出了点问题。我该如何更改我的Pet类或集合实现,以允许同时包含狗和猫饮食(或与此相关的任何其他将来的宠物衍生物)的列表?

解决方法

您需要协变:

请注意,这意味着您必须创建接口IDiet

的集合
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace ConsoleApp8
{

    public class Pet {  }
    public class Dog : Pet {  }
    public class Cat : Pet { }

    // Meals and diets

    public interface IMeal<out T> : IReadOnlyCollection<T> where T : Pet
    {
    }

    public interface IDiet<out T> : IReadOnlyCollection<IMeal<T>> where T : Pet
    {
    }

    public class Meal<T> : Collection<T>,IMeal<T> where T : Pet
    {
        public List<T> AllowedPets { get; set; } = new List<T>();
    }
    public class Diet<T> : Collection<IMeal<T>>,IDiet<T> where T : Pet
    {
        public List<Meal<T>> DietMeals { get; set; } = new List<Meal<T>>();
    }

    class Program
    {
        static void Main(string[] args)
        {
            Dog labrador = new Dog() { };
            Dog dalmatian = new Dog() { };
            Meal<Dog> mediumDogBreakfast = new Meal<Dog>{ labrador,dalmatian };
            Meal<Dog> mediumDogLunch = new Meal<Dog>{ labrador,dalmatian };
            Meal<Dog> mediumDogDinner = new Meal<Dog>{ labrador,dalmatian };
            Diet<Dog> mediumDogDiet = new Diet<Dog>()
            {
                mediumDogBreakfast,mediumDogLunch,mediumDogDinner
            };

            // Cats and their diets
            Cat siamese = new Cat() { };
            Cat sphynx = new Cat() { };
            Meal<Cat> orientalCatBreakfast = new Meal<Cat>(){ siamese,sphynx };
            Meal<Cat> orientalCatLunch = new Meal<Cat>(){ siamese,sphynx };
            Meal<Cat> orientalCatDinner = new Meal<Cat>(){ siamese,sphynx };
            Diet<Cat> orientalCatDiet = new Diet<Cat>()
            {
                orientalCatBreakfast,orientalCatLunch,orientalCatDinner
            };

            List<IDiet<Pet>> diets = new List<IDiet<Pet>>
            {
                mediumDogDiet,orientalCatDiet
            };
        }
    }
}