首页 > 代码库 > 自动从DataRow里取数据给属性赋值

自动从DataRow里取数据给属性赋值

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);      

       }       

  }

}