我有一些代表數據庫表的類,爲了加載每個表的行在DataGridView
,我有一個List<>
函數,在循環內獲取該表中的所有行。如何動態指定List <>函數的類型?
public List<class_Table1> list_rows_table1()
{
// class_Table1 contains each column of table as public property
List<class_Table1> myList = new List<class_Table1>();
// sp_List_Rows: stored procedure that lists data
// from Table1 with some conditions or filters
Connection cnx = new Connection;
Command cmd = new Command(sp_List_Rows, cnx);
cnx.Open;
IDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
class_Table1 ct = new class_Table1();
ct.ID = Convert.ToInt32(dr[ID_table1]);
ct.Name = dr[name_table1].ToString();
//... all others wanted columns follow here
myList.Add(ct);
}
dr.Close();
cnx.Close();
// myList contains all wanted rows; from a Form fills a dataGridView
return myList();
}
而對於其他表,其他一些功能:list_rows_table2,list_rows_table3 ... 我的問題是:如何創建一個只List<>
功能,在那裏我可以動態指定類型的List<>
返回,或者如何在返回之前將例如List<object>
轉換爲List<myClass>
。
這基本上是ORM的功能。爲什麼不使用ORM?實體框架運行得非常好,我們將它用在一個大型的LOB應用程序中,有400多個客戶運行一個SAAS應用程序(每個平臺有3個以上的計算機),服務器端託管在我們的服務器中。 –
看一看[ValueInjecter](http://valueinjecter.codeplex.com/),特別是[本示例](http://goo.gl/mD5OG),它會將數據讀取器映射到域對象列表就像你想要做的那樣。敬意和新年快樂! – Hugo