首页 > 代码库 > 一段关于实体类映射的代码

一段关于实体类映射的代码

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;
        }

 

一段关于实体类映射的代码