如何创建采用Generic基类型的方法

时间:2012-02-07 09:31:09

标签: c# generics

我有类似于以下内容的内容,但我无法向House方法提供FarmPrintShelterAddress个对象:

public interface IAnimal { };
public interface IDomesticAnimal : IAnimal { };
public interface IHouseAnimal : IDomesticAnimal { };
public interface IFarmAnimal : IDomesticAnimal { };

public class Animal : IAnimal { }
public class DomesticAnimal : Animal, IDomesticAnimal { }
public class Lion : Animal { }
public class Cat : DomesticAnimal, IHouseAnimal { }
public class Horse : DomesticAnimal, IFarmAnimal { }

public interface IAnimalShelter<T> where T : IDomesticAnimal { String Address { get; set; } };
public interface IHouse : IAnimalShelter<IHouseAnimal> { };
public interface IFarm : IAnimalShelter<IFarmAnimal> { };

public class AnimalShelter<T> : IAnimalShelter<T> where T : IDomesticAnimal { public String Address { get; set; } }
public class House : AnimalShelter<IHouseAnimal>, IHouse { }
public class Farm : AnimalShelter<IFarmAnimal>, IFarm { }

class Program
{
    static void Main(string[] args)
    {
        PrintShelterAddress(new House() { Address = "MyHouse" });  // Error: argument type 'House' is not assignable to parameter type 'IAnimalShelter<IDomesticAnimal>'

        // This makes sense as House is a IAnimalShelter<IHouseAnimal>
        // and IHouseAnimal cannot be cast to its parent IDomesticAnimal
        IAnimalShelter<IDomesticAnimal> nonDescriptShelter = new House();  // InvalidCastException: Unable to cast object of type 'House' to type 'IAnimalShelter`1[IDomesticAnimal]'.
    }

    static void PrintShelterAddress(IAnimalShelter<IDomesticAnimal> nonDescriptShelter)
    {
        Console.WriteLine(nonDescriptShelter.Address as string);
    }
}

我尝试了什么:

手工演员:

PrintShelterAddress((IAnimalShelter<IDomesticAnimal>)new House() { Address = "MyHouse" });

按预期编译,抛出运行时异常:无法将类型为'House'的对象强制转换为'IAnimalShelter`1 [IDomesticAnimal]'。

我还尝试了什么:

static void PrintShelterAddress(dynamic nonDescriptShelter)
{
    Console.WriteLine(nonDescriptShelter.Address);
}

这有效,但我并不热衷于使用dynamic

我最好的解决方案:

将非通用基本接口添加到IAnimalShelter<T>并使用:

public interface IAnimalShelter { String Address { get; set; } };
public interface IAnimalShelter<T> : IAnimalShelter where T : IDomesticAnimal { };

static void PrintShelterAddress(IAnimalShelter nonDescriptShelter) { ... }

因此...

是否有比使用dynamic或向IAnimalShelter<T>添加基础界面更好的解决方案?

1 个答案:

答案 0 :(得分:3)

嗯..尝试使你的接口协变:

public interface IAnimalShelter<out T> : .....