public abstract class ModelsBase {
//自动从DataRow里取数据给属性赋值
public void RightDataFrom(DataRow row)
{
Type type = this.GetType();
//遍历每一个属性
foreach (PropertyInfo prop in type.GetProperties())
{
//判断是否可写入
if (prop.CanWrite == false)
continue;
//判断DataRow里是否包含该属性的值
if (row.Table.Columns.Contains(prop.Name) == false)
continue;
//是否为空
if (row[prop.Name] == DBNull.Value)
continue;
//赋值
prop.SetValue(this, row[prop.Name], null);
}
}
}
自动从DataRow里取数据给属性赋值,布布扣,bubuko.com
原文:http://www.cnblogs.com/gzh-ITroom/p/3759465.html