我已經創建了一個接口和類,如下所示:需要一個通用的轉換爲它的接口
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>'
我可以修改我的代碼返回正確的表格?
謝謝,馬特,這正是我一直在尋找的! – 2014-09-11 06:55:47