zoukankan      html  css  js  c++  java
  • 一般处理程序的救星!利用反射一招叫你摆脱丑比代码

    我们一般做异步请求数据的时候,都是通过post或者get提交数据。
    我们后台接收post或者get数据的方式一般是这样的。。。

                int companyId = int.Parse(context.Request.Form["CompanyId"]); //公司ID
                int goodsTypeId = int.Parse(context.Request.Form["GoodsType"]); //产品类别
                string goodsName = context.Request.Form["GoodsName"]; //产品名称
                decimal nowPrice = decimal.Parse(context.Request.Form["NowPrice"]); //现价
                decimal costPrice = decimal.Parse(context.Request.Form["CostPrice"]);//成本价
                int goodsSubType = int.Parse(context.Request.Form["GoodsSubType"]); //是否是相册类产品
                decimal albumPagePrice = decimal.Parse(context.Request.Form["AlbumPagePrice"]); //每P单价
                int defaultPageNumber = int.Parse(context.Request.Form["DefaultPageNumber"]);//默认P数
                int goodsSort = int.Parse(context.Request.Form["GoodsSort"]);//排序
                string remark = context.Request.Form["Remark"]; //备注
                string branchIds = context.Request.Form["BranchIds"];//分店信息
    

    一般我们拿到post过来的数据,然后赋值给实体。具体代码一般是这样。。

                int companyId = int.Parse(context.Request.Form["CompanyId"]); //公司ID
                int goodsTypeId = int.Parse(context.Request.Form["GoodsType"]); //产品类别
                string goodsName = context.Request.Form["GoodsName"]; //产品名称
                decimal nowPrice = decimal.Parse(context.Request.Form["NowPrice"]); //现价
                decimal costPrice = decimal.Parse(context.Request.Form["CostPrice"]);//成本价
                int goodsSubType = int.Parse(context.Request.Form["GoodsSubType"]); //是否是相册类产品
                decimal albumPagePrice = decimal.Parse(context.Request.Form["AlbumPagePrice"]); //每P单价
                int defaultPageNumber = int.Parse(context.Request.Form["DefaultPageNumber"]);//默认P数
                int goodsSort = int.Parse(context.Request.Form["GoodsSort"]);//排序
                string remark = context.Request.Form["Remark"]; //备注
                string branchIds = context.Request.Form["BranchIds"];//分店信息
    
    
                Models.Goods.GoodsInfo model = new Models.Goods.GoodsInfo();
    
                model.GoodsTypeId = goodsTypeId;
                model.GoodsName = goodsName;
                model.NowPrice = nowPrice;
                model.CostPrice = costPrice;
                model.GoodsSubType = goodsSubType;
                model.AlbumPagePrice = albumPagePrice;
                model.DefaultPageNumber = defaultPageNumber;
                model.GoodsSort = goodsSort;
                model.GoodDescription = remark;
                model.BranchIds = branchIds;
                model.CompanyId = companyId;
    

    如果post过来30个参数,就要拼30遍,你们说这代码 # 丑比不丑比!!!!
    来,我用一招拯救你。
    我上面的代码,可以用两句代码来表示。

    
                Models.Goods.GoodsInfo model = new Models.Goods.GoodsInfo();
                model = KLY.Common.RequestDataHelper<Models.Goods.GoodsInfo>.PostDataToModel(context, model);
    

    具体就是RequestDataHelper 这个帮助类。
    通过反射把post过来的数据,直接转换成实体。
    RequestDataHelper帮助类代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web;
    
    namespace KLY.Common
    {
        public class RequestDataHelper<T>
        {
            /// <summary>
            /// 
            /// </summary>
            /// <param name="context"></param>
            /// <param name="model"></param>
            /// <returns></returns>
            public static T PostDataToModel(HttpContext context,T model)
            {
                Type t = model.GetType();
                PropertyInfo[] pis = t.GetProperties();
                foreach (PropertyInfo pi in pis)
                {
                    if (!string.IsNullOrEmpty(context.Request.Form[pi.Name]))
                    {
                        Type type = pi.PropertyType;
                        Type underlyingType = Nullable.GetUnderlyingType(type);
                        pi.SetValue(model, Convert.ChangeType(context.Request.Form[pi.Name],underlyingType??type));
                    }
                }
                return model;
            }
        }
    }
    
    

    不过我这种有局限性,只适用于实体的属性和post的参数名字一致的情况。(像我这种人,一般都是代码生成器生成,所以一般都是一致的。。。)

    修改记录

    2016.04.18 当实体有null的情况,转换成实体失败问题修改。
    参考文献。
    http://www.cnblogs.com/patrickyu/p/3211115.html
    修改记录:

                        Type type = pi.PropertyType;
                        Type underlyingType = Nullable.GetUnderlyingType(type);
                        pi.SetValue(model, Convert.ChangeType(context.Request.Form[pi.Name],underlyingType??type));
    

    文章如果对你有帮助,来点个赞~
    如果有意见,来吐个槽也行!_

  • 相关阅读:
    bzoj3884: 上帝与集合的正确用法(数论)
    洛谷10月月赛R2·浴谷八连测R3题解
    bzoj5055: 膜法师(BIT)
    bzoj2213: [Poi2011]Difference(思维题)
    bzoj1016: [JSOI2008]最小生成树计数(kruskal+dfs)
    一模 (2) day2
    一模 (2) day1
    Prime Palindromes
    常州培训 day5 解题报告
    一模 (1) day2
  • 原文地址:https://www.cnblogs.com/woaic/p/5397177.html
Copyright © 2011-2022 走看看