即时尝试从数据库动态获取数据。我已经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