即時嘗試從數據庫動態獲取數據。我已經list<T> select<T>()
方法......我的繼承人問題,我可以在這裏使用的DataReader得到DATAS是代碼:如何在列表中添加類型<T> in c#4,0
public list<T> Select<T>()
{
Type type = typeof(T);
...
...
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
try
{
SqlCommand sqlCommand = new SqlCommand(selectCommand, connection);
connection.Open();
SqlDataReader sqlReader = sqlCommand.ExecuteReader();
while (sqlReader.Read())
{
foreach (PropertyInfo property in type.GetProperties())
{
property.SetValue(property.Name,(PropertyInfo)sqlReader[property.Name],null);
}
typeList.Add((T)Convert.ChangeType(type,typeof(T)));
}
sqlReader.Close();
}
catch (Exception ex)
{
string exc = ex.Message;
}
finally
{
connection.Close();
}
}
return typeList;
}
我可以得到的數據,但我不能把它分配給我喜歡的類型,所以我不能加我型到我的類型串,可以幫助我
請編輯你質疑,請選擇您的代碼,然後按在按鈕的代碼編輯(101 \ 010)。 – 2010-08-22 10:05:47
您的代碼不完整,缺少typeList的聲明。 – 2010-08-22 10:08:43