前台如果只传递了一两个数据,在后台请求获取数据设置属性值还能够接受,但是如果传递了大量的数据,就得多次HttpRequest reques[“XXX”]请求获取数据设置属性值,输入的代码量就很大,而且相当耗费时间,这种费时费力的方法是难以接受的。下面为大家提供一个取巧的方法,获取请求数据就变的很简单了,而且可以节省时间。话不多说,直接上代码:
C#代码如下:
/// <summary> /// 设置对象属性——反射实体设置变量 /// </summary> /// <param name="obj">对象</param> /// <param name="set">设置属性操作</param>
protected void Set_Property(object obj,Del_Property set) { string param = string.Empty,type; PropertyInfo[] propertys = obj.GetType().GetProperties(); foreach (PropertyInfo p in propertys){ //获取请求参数 object[] columns = p.GetCustomAttributes(typeof(JsonPropertyAttribute), true); if (columns.Length > 0){ //设置属性值 param = request[(columns[0] as JsonPropertyAttribute).PropertyName]; if (param != null){ type = p.PropertyType.ToString(); type = Regex.Match(type, type.IndexOf("[") > -1 ? @"(?<=\[\w+\.)[^\]]+" : @"(?<=\.)\w+").Value; switch (type) { case "Boolean": p.SetValue(obj, Boolean.Parse(param), null); break; case "Byte": p.SetValue(obj, Byte.Parse(param), null); break; case "Decimal": p.SetValue(obj, Decimal.Parse(param), null); break; case "DateTime": p.SetValue(obj, DateTime.Parse(param), null); break; case "Int16": p.SetValue(obj, Int16.Parse(param), null); break; case "Int32": p.SetValue(obj, Int32.Parse(param), null); break; case "Int64": p.SetValue(obj, Int64.Parse(param), null); break; case "Single": p.SetValue(obj, Single.Parse(param), null); break; case "Entity": ; break; default: p.SetValue(obj, param, null); break; } } } } if (set != null) set(obj); }
示例:
using System; using System.Data; using System.Collections.Generic; using DataObject.Attrib; using Newtonsoft.Json; //部分实体 namespace TiaoCeng.Entity { /// <summary> /// 订单 /// </summary> [DBTable(Name = "[Orders]",PrimaryKey="or_id")] public class OrdersModel : DataObject.DataFactory.DataModule<OrdersModel> { /// <summary> ///订单构造函数 /// </summary> public OrdersModel() : base() { } /// <summary> ///订单构造函数 /// </summary> /// <param name="constring">数据库连接字符串</param> public OrdersModel(string constring) : base(constring) { } /// <summary> /// 订单id /// </summary> [DBColumn(Name = "or_id", Size = 4, dbType = SqlDbType.Int, PrimaryKey = true)] [JsonProperty(PropertyName = "or_id")] public int? Or_id { get; set;} /// <summary> /// 购买者id,为0表示为非用户id /// </summary> [DBColumn(Name = "m_id", Size = 4, dbType = SqlDbType.Int)] [JsonProperty(PropertyName = "member")] public MemberModel Member { get; set; } /// <summary> /// 购买者联系电话 /// </summary> [DBColumn(Name = "or_buyerTel", Size = 50, dbType = SqlDbType.VarChar)] [JsonProperty(PropertyName = "or_buyertel")] public string Or_buyerTel { get; set;} /// <summary> /// 购买者地址 /// </summary> [DBColumn(Name = "or_buyerAddress", Size = 200, dbType = SqlDbType.NVarChar)] [JsonProperty(PropertyName = "or_buyeraddress")] public string Or_buyerAddress { get; set;}/// <summary> /// 已付款金额 /// </summary> [DBColumn(Name = "or_payPrice", Size = 9, dbType = SqlDbType.Decimal)] [JsonProperty(PropertyName = "or_payprice")] public decimal? Or_payPrice { get; set;}/// <summary> /// 订单状态(0购物车,1待核实, 2待付款,3付款成功,4已发货,5确认收货,6申请退款,7退款成功,8退款失败) /// </summary> [DBColumn(Name = "or_progressState", Size = 4, dbType = SqlDbType.Int)] [JsonProperty(PropertyName = "or_progressstate")] public int? Or_progressState { get; set;} /// <summary> /// 订单进度说明 /// </summary> [DBColumn(Name = "or_progress", Size = 1000, dbType = SqlDbType.NVarChar)] [JsonProperty(PropertyName = "or_progress")] public string Or_progress { get; set;}/// <summary> /// 状态(0待审核,1审核通过,2审核失败,3回收站,4.作废) /// </summary> [DBColumn(Name = "or_state", Size = 1, dbType = SqlDbType.TinyInt)] [JsonProperty(PropertyName = "or_state")] public byte? Or_state { get; set; } .... }
//实体对象OrdersModel _order = new OrdersModel();//设置订单信息 Set_Property(_order, (obj) => { _order.Member = new MemberModel() { M_id = m_id }; if (!string.IsNullOrEmpty(request["paydate"])) _order.Or_payDate = DateTime.Parse(request["paydate"] + " 00:00:00"); if (!string.IsNullOrEmpty(request["shipdate"])) _order.Or_shipDate = DateTime.Parse(request["shipdate"] + " 00:00:00"); _order.Or_price = _price; _order.Or_amount = _num; _order.Or_getIntegral = _getIntegral; });
这样一来,不管前台传递多少数据,后台只需要将实体对象传递进去就可以省时省力的完成设置属性值的操作,当然前台的请求参数要与实体的拓展属性名保持一致。是不是很方便,快来试一试吧。有什么好的方法或者建议可以联系我,我们一起探讨。
新浪微博(求关注)地址:
时间: 2024-11-10 10:33:00