首页 > 代码库 > 一段关于实体类映射的代码
一段关于实体类映射的代码
handler层:
#region 根据市场费用编号删除制定市场费用 private void DelCommissionByCommissionID(HttpRequest Request, HttpContext context) { int pindex = 0; if (Request["pindex"] != "" && !string.IsNullOrEmpty(Request["pindex"])) { pindex = Convert.ToInt32(Request["pindex"]); } //验证权限 UserControler control = new UserControler(context); //映射实体类Commission object obj = Utility.InitObject(Request, "Contract.Model.dll", "Contract.Model.Commission"); Model.Commission commission = obj as Model.Commission; //ContractControler controler = ContractControler.GetContractByID(control.UserID, commission.ContractID); ContractControler controler = ContractControler.GetContractByID(control.UserID, commission.ContractID, pindex, 10, control.DepartmentID); int result = controler.DeleteCommission(commission); var json = new { Status = "failed", Msg = "删除失败" }; if (result > 0) { json = new { Status = "success", Msg = "删除成功"}; } context.Response.Write(js.Serialize(json)); } #endregion
具体方法:
/// <summary> /// 用页面request中的参数实例化对象,要求页面中的参数名必须和对象中的属性名一致 /// </summary> /// <param name="Request"></param> /// <param name="assemblyName">要求是dll文件名</param> /// <param name="typeName">对象名,要求对象必须有公用的无参数的实例化方法</param> /// <returns></returns> public static Object InitObject(HttpRequest Request, string assemblyName, string typeName) { Assembly assembly = Assembly.LoadFrom(Request.PhysicalApplicationPath + "\\bin\\" + assemblyName); Type type = assembly.GetType(typeName); //Type type = Type.GetType(typeName); Object obj = System.Activator.CreateInstance(type); foreach (var item in type.GetProperties()) { string temp = Request[item.Name]; if (item.PropertyType == typeof(string)) item.SetValue(obj, temp, null); if ((item.PropertyType == typeof(int) || item.PropertyType == typeof(int?)) && temp != null && temp != "") { try { int intTmp = 0; int.TryParse(temp, out intTmp); item.SetValue(obj, intTmp, null); } catch { } } if (item.PropertyType == typeof(DateTime?) || item.PropertyType == typeof(DateTime)) { try { DateTime tmpd = DateTime.Now; DateTime.TryParse(temp, out tmpd); if (tmpd == DateTime.MinValue) tmpd = DateTime.Now; item.SetValue(obj, tmpd, null); } catch { } } if (item.PropertyType == typeof(Decimal) && temp != null && temp != "") { try { Decimal d = 0; Decimal.TryParse(temp, out d); item.SetValue(obj, d, null); } catch { } } if (item.PropertyType == typeof(Boolean) && temp != null && temp != "") { try { bool d = false; Boolean.TryParse(temp, out d); item.SetValue(obj, d, null); } catch { } } } return obj; }
一段关于实体类映射的代码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。