我创建了以下接口和类:
public interface IRecord
{
}
public class Person : IRecord
{
}
public class Company : IRecord
{
}
public class Contact : IRecord
{
}
public class Table<T> where T : IRecord
{
}
我还有一个用于管理表格的静态类。我希望能够根据 switch 语句获取特定的表格:
public class DataBase
{
public static Table<Person> Persons { get; set; }
public static Table<Contact> Contacts { get; set; }
public static Table<Company> Companies { get; set; }
public static Table<IRecord> GetTable(eDataType type)
{
switch (type)
{
case eDataType.company:
return Companies;
case eDataType.contact:
return Contacts;
case eDataType.person:
return Persons;
}
return null;
}
}
然而,所有的返回行都会抛出编译错误:
Cannot implicitly convert type 'Table<Company>' to 'Table<IRecord>'
我能修改我的代码以返回正确的表格吗?