对一个对象实体进行赋值

        /// <summary>
        /// 对一个对象实体进行赋值
        /// </summary>
        /// <param name="obj">对象实体</param>
        /// <param name="row">行数据</param>
        /// <param name="Fields">查询字段</param>
        /// <returns>赋值后的对象实体</returns>
        public static object DataRowToModel(object obj, System.Data.DataRow row, ref string errMsg, params string[] Fields)
        {
            if (obj == null || row == null || Fields == null || Fields.Length <= 0)
            {
                errMsg = "传入参数不完整!";
                return null;
            }
            try
            {

                for (int i = 0, j = Fields.Length; i < j; i++)
                {
                    string field = Fields[i].ToString();//字段
                    bool exist = row.Table.Columns.Contains(field);//是否存在
                    if (exist && row[field] != null)
                    {
                        string value = row[field].ToString();
                        System.Reflection.PropertyInfo[] fields = obj.GetType().GetProperties();
                        foreach (System.Reflection.PropertyInfo pt in fields)
                        {
                            if (pt.Name.ToUpper().Equals(field.ToUpper()))
                            {
                                string tp = pt.PropertyType.FullName;

                                #region 数据类型转换
                                switch (tp)
                                {
                                    case "System.DateTime":
                                    case "System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]":
                                    case "System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]":
                                        { //可空DateTime类型
                                            pt.SetValue(obj, Convert.ToDateTime(value), null);
                                        }
                                        break;
                                    case "System.Int64":
                                    case "System.Nullable`1[[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]":
                                    case "System.Nullable`1[[System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]":
                                        { //可空int类型
                                            pt.SetValue(obj, Convert.ToInt64(value), null);
                                        }
                                        break;
                                    case "System.Int32":
                                    case "System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]":
                                    case "System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]":
                                        { //可空int类型
                                            pt.SetValue(obj, Convert.ToInt32(value), null);
                                        }
                                        break;
                                    case "System.Decimal":
                                    case "System.Nullable`1[[System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]":
                                    case "System.Nullable`1[[System.Decimal, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]":
                                        { //可空Decimal类型
                                            pt.SetValue(obj, Convert.ToDecimal(value), null);
                                        }
                                        break;
                                    case "System.Boolean":
                                    case "System.Nullable`1[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]":
                                    case "System.Nullable`1[[System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]":
                                        { //可空Boolean类型
                                            pt.SetValue(obj, Convert.ToBoolean(value), null);
                                        }
                                        break;
                                    case "System.Double":
                                        {
                                            pt.SetValue(obj, Convert.ToDouble(value), null);
                                        }
                                        break;
                                    case "System.Char":
                                        {
                                            pt.SetValue(obj, Convert.ToChar(value), null);
                                        }
                                        break;
                                    case "System.String":
                                        {
                                            pt.SetValue(obj, Convert.ToString(value), null);
                                        }
                                        break;
                                    default:
                                        pt.SetValue(obj, value, null);
                                        break;
                                }
                                #endregion
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                Utils.LogHelper.Error("DataRowToModel", "实体转换出错", "异常错误:" + ex.ToString());
                return null;
            }

            return obj;
        }

 

posted @ 2016-06-14 19:31 武穆逸仙 阅读(...) 评论(...) 编辑 收藏