• Request.From,Request.QueryString转对象


    From表单转化为对象

            public static T RequestFormEntities<T>(HttpRequestBase request) where T : new()
            {
                T entity = Activator.CreateInstance<T>();
                PropertyInfo[] attrs = entity.GetType().GetProperties();
                foreach (PropertyInfo p in attrs)
                {
                    foreach (string key in request.Form.AllKeys)
                    {
                        if (string.Compare(p.Name, key, true) == 0)
                        {
                            try
                            {
                                Type type = p.PropertyType;
                                //判断type类型是否为泛型,因为nullable是泛型类
                                if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))//判断convertsionType是否为nullable泛型类  
                                {
                                    //如果type为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换  
                                    System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(type);
                                    //将type转换为nullable对的基础基元类型  
                                    type = nullableConverter.UnderlyingType;
                                }
    
                                p.SetValue(entity, Convert.ChangeType(request.Form[key], type), null);
                            }
                            catch (Exception e)
                            {
                                continue;
                            }
                        }
                    }
                }
                return entity;
            }
    View Code

    Request参数转化为对象

             public static T RequestStringFormEntities<T>(string request) where T : new()
            {
                T entity = Activator.CreateInstance<T>();
                PropertyInfo[] attrs = entity.GetType().GetProperties();
                foreach (PropertyInfo p in attrs)
                {
                    foreach (string key in request.Split('&'))
                    {
                        var _key = key.Split('=')[0];
                        var _value = key.Split('=')[1];
                        if (string.Compare(p.Name, _key, true) == 0)
                        {
                            try
                            {
                                Type type = p.PropertyType;
                                //判断type类型是否为泛型,因为nullable是泛型类
                                if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))//判断convertsionType是否为nullable泛型类  
                                {
                                    //如果type为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换  
                                    System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(type);
                                    //将type转换为nullable对的基础基元类型  
                                    type = nullableConverter.UnderlyingType;
                                }
    
                                p.SetValue(entity, Convert.ChangeType(_value, type), null);
                            }
                            catch (Exception e)
                            {
                                continue;
                            }
                        }
                    }
                }
                return entity;
            }
    View Code
  • 相关阅读:
    P3193 [HNOI2008]GT考试(KMP+矩阵乘法加速dp)
    P2606 [ZJOI2010]排列计数
    洛谷P2657 [SCOI2009]windy数
    P2602 [ZJOI2010]数字计数(递推)
    洛谷 P1073 最优贸易
    [一本通学习笔记] 字典树与 0-1 Trie
    [一本通学习笔记] KMP算法
    [一本通学习笔记] 字符串哈希
    [一本通学习笔记] 广度优先搜索和优化
    [一本通学习笔记] 深度优先搜索与剪枝
  • 原文地址:https://www.cnblogs.com/plming/p/8142949.html
Copyright © 2020-2023  润新知