我创建了一个属性类,表示我的SQLite数据库的表模式。类中的每个属性都将成为数据库中的一个属性。
class SQLiteTables
{
public class tblPerson
{
[PrimaryKey, AutoIncrement]
public int PersonID { get; set; }
public string PersonFirstName { get; set; }
public string PersonMiddleName { get; set; }
public string PersonLastName { get; set; }
}
public class tblSchedule
{
[PrimaryKey, AutoIncrement]
public int ScheduleID { get; set; }
public string ScheduleDescription { get; set; }
public DateTime ScheduleStart { get; set; }
public DateTime ScheduleEnd { get; set; }
}
//18 more table definitions below
}
现在,要从表中检索一些数据,
public void GetPersonList()
{
List<tblPerson> lstTemp = dbConn.Table<tblPerson>().ToList<tblPerson>();
// Some 20 lines of code here
}
我想避免编写特定于类型的方法,这是一个繁琐的任务。我想创建一个可重用的方法,该方法可以使用泛型实现检索任何表上的数据。我该如何实现?
编辑我正在Windows Phone 8平台上工作。
最佳答案
这应该适合您的工作。
public List<T> GetItems<T>()
{
return dbConn.Table<T>().ToList<T>();
}
关于c# - 自动实现的属性类的C#泛型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21821713/