您的位置:首页 > 其它

利用反射快速给Model实体赋值

2013-01-15 16:50 369 查看
public class BaseModel<T> where T : new()
{
public static T Init(DataRow dr)
{
T t = new T();
Type infotype = typeof(T);
//获取所有属性
PropertyInfo[] propertys = infotype.GetProperties();
//遍历属性并将datarow中字段的值赋值给同名属性
foreach (PropertyInfo pi in propertys)
{
infotype.GetProperty(pi.Name).SetValue(t, result, null);
}
return t;
}
}

BaseModel<Student>.Init(dr)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: