ASP.NET From表单转实体类

原由

经常遇到 int Age=Convert.ToInt32(this.txtAge.Text);

这种蛋疼的代码,特写次方法。

之所以抛出异常是希望知道转换失败,格式错误的属性是什么,方便调试。

新版本

//新版本,可以再globa.ascx里面设置开发模式
//以便于调试错误
//表单转实体对象V2版
public class HttpRequestHelper
    {
        /// <summary>
        /// 开发模式,设置成开发模式后抛出异常,可查看出错的属性,和类型
        /// </summary>
        public static bool DevMode { get; set; }
        #region 表单转换成实体模型
        /// <summary>
        /// 表单转换成实体模型
        /// 用法:1、插入,先new一个对象传入;
        ///       2、更新,先从数据库查出这个对象传入
        /// </summary>
        /// <param name="obj">泛型对象</param>
        /// <returns></returns>
        public static T ConvertForm2Model<T>(T obj)
        {
            if (null == obj)
            {
                obj = default(T);
                obj = Activator.CreateInstance<T>();
            }
            Type type = typeof(T);
            foreach (var p in type.GetProperties())
            {
                string result = HttpContext.Current.Request.Params[p.Name];
                if (!string.IsNullOrWhiteSpace(result))
                {
                   try
                   {
                       p.SetValue(obj, Convert.ChangeType(result, p.PropertyType), null);
                   }
                    catch(Exception ex)
                   {
                       if (DevMode)
                       {
                           throw new Exception("类型转换错误,变量:" + p.Name + ",类型:" + p.PropertyType + ",错误信息:" + ex.Message);
                       }
                       else
                       {
                           //记录日志。。。。

                       }
                   }
                }
            }
            return obj;

        }
        #endregion
    }

旧版本

  
//////////////////
///各位有什么好想法,请给个建议
//460247986@qq.com
//////////////////

public class HttpRequestHelper<T> where T : class,new()
    {
        #region 表单转换成实体模型
        /// <summary>
        /// 表单转换成实体模型
        /// 用法:1、插入,先new一个对象传入;
        ///       2、更新,先从数据库查出这个对象传入
        /// </summary>
        /// <param name="obj">泛型对象</param>
        /// <returns></returns>
        public static T ConvertForm2Model(T obj=null)
        {
            if (null == obj)
            {
                obj = default(T);
                obj = Activator.CreateInstance<T>();
            }
            Type type = typeof(T);
            foreach (var p in type.GetProperties())
            {
                string result = HttpContext.Current.Request.Params[p.Name];
                if (!string.IsNullOrWhiteSpace(result))
                {
                   try
                   {
                       p.SetValue(obj, Convert.ChangeType(result, p.PropertyType), null);
                   }
                    catch(Exception ex)
                   {
                       throw new Exception("类型转换错误,变量:" + p.Name + ",类型:" + p.PropertyType+",错误信息:" + ex.Message);
                   }
                }
            }
            return obj;

        }
        #endregion
    }
原文地址:https://www.cnblogs.com/shya/p/3940109.html