~~~请教IList,List分别在什么情况下使用比较合适?
finer 2017-11-18 08:36:49 像下面方法的返回类型用的是IList,里面也是IList,可以用List吗,为什么不用List的呢?谢谢
public class ModelConvertHelper<T> where T : new() // 此处一定要加上new()
{
public static IList<T> ConvertToModel(DataTable dt)
{
IList<T> ts = new List<T>();// 定义集合
Type type = typeof(T); // 获得此模型的类型
string tempName = "";
foreach (DataRow dr in dt.Rows)
{
T t = new T();
PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
foreach (PropertyInfo pi in propertys)
{
tempName = pi.Name;
if (dt.Columns.Contains(tempName))
{
if (!pi.CanWrite) continue;
object value = dr[tempName];
if (value != DBNull.Value)
pi.SetValue(t, value, null);
}
}
ts.Add(t);
}
return ts;
}
}