使用通用方法将接口作为约束类型

这是一个如何在类 Animal 上使用通用类型 TFood 的示例

public interface IFood
{
    void EatenBy(Animal animal);
}

public class Grass: IFood
{
    public void EatenBy(Animal animal)
    {
        Console.WriteLine("Grass was eaten by: {0}", animal.Name);
    }
}

public class Animal
{
    public string Name { get; set; }

    public void Eat<TFood>(TFood food)
        where TFood : IFood
    {
        food.EatenBy(this);
    }
}

public class Carnivore : Animal
{
    public Carnivore()
    {
        Name = "Carnivore";
    }
}

public class Herbivore : Animal, IFood
{
    public Herbivore()
    {
        Name = "Herbivore";
    }
    
    public void EatenBy(Animal animal)
    {
        Console.WriteLine("Herbivore was eaten by: {0}", animal.Name);
    }
}

你可以像这样调用 Eat 方法:

var grass = new Grass();        
var sheep = new Herbivore();
var lion = new Carnivore();
    
sheep.Eat(grass);
//Output: Grass was eaten by: Herbivore

lion.Eat(sheep);
//Output: Herbivore was eaten by: Carnivore

在这种情况下,如果你尝试呼叫:

sheep.Eat(lion);

这是不可能的,因为对象狮子没有实现接口 IFood。尝试进行上述调用将生成编译器错误:“类型’食肉动物’不能在泛型类型或方法’Animal.Eat(TFood)‘中用作类型参数’TFood’。没有隐式引用转换’ Carnivore’到’IFood’。“