Esempio n. 1
0
        // 传入DataRow,将其转换为ModelParameters
        private ModelParameters toModel(DataRow dr)
        {
            ModelParameters model = new ModelParameters();

            //通过循环为ModelParameters赋值,其中为数据值为空时,DateTime类型的空值为:0001/1/1 0:00:00    int类型得空值为: 0,其余的还没试验
            foreach (PropertyInfo propertyInfo in typeof(ModelParameters).GetProperties())
            {
                //如果数据库的字段为空,跳过其赋值
                if (dr[propertyInfo.Name].ToString() == "")
                {
                    continue;
                }
                //赋值
                model.GetType().GetProperty(propertyInfo.Name).SetValue(model, dr[propertyInfo.Name], null);
            }
            return(model);
        }