码迷,mamicode.com
首页 > 其他好文 > 详细

Request.From,Request.QueryString转对象

时间:2017-12-29 10:16:14      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:amp   分享图片   ret   each   http   continue   converter   hid   string   

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

 

Request.From,Request.QueryString转对象

标签:amp   分享图片   ret   each   http   continue   converter   hid   string   

原文地址:https://www.cnblogs.com/plming/p/8142949.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!