- public static List<T2> ExcuteList<T2>(string strSelectCmd, params SqlParameter[] paras)
- {
- using (SqlConnection conn = new SqlConnection(connStr))
- {
- SqlDataAdapter da = new SqlDataAdapter(strSelectCmd, conn);
- da.SelectCommand.Parameters.AddRange(paras);
- DataTable dt = new DataTable();
- da.Fill(dt);
- //将DataTable转成泛型集合List<T2>
- if (dt.Rows.Count > 0)
- {
- List<T2> list = new List<T2>();
- foreach (DataRow row in dt.Rows)
- {
- //(1)获得泛型类型
- Type t = typeof(T2);
- //(2)根据类型创建该类型的对象
- T2 model = (T2)Activator.CreateInstance(t);
- //(3)根据类型 获得 该类型所有属性定义
- PropertyInfo[] properties = t.GetProperties();
- //(4)遍历属性数组
- foreach (PropertyInfo p in properties)
- {
- string colName = p.Name;
- object colValue = row[colName];
- p.SetValue(model, colValue, null);
- }
- list.Add(model);
- }
- return list;
- }
- }
- return null;
- }
复制代码 我弄不懂,谁给讲讲啊
//(2)根据类型创建该类型的对象
T2 model = (T2)Activator.CreateInstance(t);
//(3)根据类型 获得 该类型所有属性定义
PropertyInfo[] properties = t.GetProperties();
//(4)遍历属性数组
foreach (PropertyInfo p in properties)
{
string colName = p.Name;
object colValue = row[colName];
p.SetValue(model, colValue, null);
}
list.Add(model); |