zoukankan      html  css  js  c++  java
  • 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
        }
  • 相关阅读:
    7-4 找出不是两个数组共有的元素(20 分)
    7-2 删除重复字符(20 分)
    7-1 兔子繁衍问题(15 分)
    1018 Public Bike Management (30分) (迪杰斯特拉+dfs)
    PAT 1014 Waiting in Line (30分) 一个简单的思路
    1010 Radix (25分)
    试题编号: 201809-4 试题名称: 再卖菜 记忆化搜索
    试题编号: 201903-3 试题名称: 损坏的RAID5
    CCF 试题编号: 201909-4 试题名称: 推荐系统
    洛谷P3809 后缀数组模板
  • 原文地址:https://www.cnblogs.com/shya/p/3940109.html
Copyright © 2011-2022 走看看