zoukankan      html  css  js  c++  java
  • 一段关于实体类映射的代码

    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;
            }
  • 相关阅读:
    java解析xml
    支持向量机SVM
    资源-菜单
    GIT
    基于OpenCV的图书扫描识别程序开发
    最大公约数(gcd)还有最小公倍数(lcm)的共通之处
    python网页分析
    python爬虫的基本知识储备
    大数加法 (A + B Problem II)
    Andy's First Dictionary (set)
  • 原文地址:https://www.cnblogs.com/chzbgb/p/6801085.html
Copyright © 2011-2022 走看看