HTTP上下文表单内容转为实体对象

using ServiceStack.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace restService.Interface.Helper
{
    public static class EntityHelper
    {
        /// <summary>
        /// 将HTTP上下文表单内容转为实体对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="context"></param>
        /// <returns></returns>
        public static T RequestToModel<T>(this HttpContext context) where T : new()
        {
            T model = new T();
            Type type = model.GetType();
            PropertyInfo[] Fields = type.GetProperties();
            for (int i = 0; i < Fields.Count(); i++)
            {
                var name = Fields[i].Name;
                Type vtype = Fields[i].PropertyType;
                var value = context.Request[name];
                if (value != null)
                {
                    if (vtype.Name.ToLower().IndexOf("int") >= 0)//int
                    {
                        var v = Convert.ToInt32(value);
                        Fields[i].SetValue(model, v);
                        continue;
                    }
                    if (vtype.Name.ToLower().IndexOf("string") >= 0)//string
                    {
                        var v = Convert.ToString(value);
                        Fields[i].SetValue(model, v);
                        continue;
                    }
                    if (vtype.Name.ToLower().IndexOf("datetime") >= 0)//datetime
                    {
                        var v = Convert.ToDateTime(value);
                        Fields[i].SetValue(model, v);
                    }
                    if (vtype.Name.ToLower().IndexOf("bool") >= 0)//datetime
                    {
                        var v = Convert.ToBoolean(value);
                        Fields[i].SetValue(model, v);
                    }
                }
            }
            return model;
        }
        /// <summary>
        /// 将HTTP上下文表单内容转为实体对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="context"></param>
        /// <returns></returns>
        public static T RequestToModel<T>(this IRequest context) where T : new()
        {
            T model = new T();
            Type type = model.GetType();
            PropertyInfo[] Fields = type.GetProperties();
            for (int i = 0; i < Fields.Count(); i++)
            {
                var name = Fields[i].Name;
                Type vtype = Fields[i].PropertyType;
                string value = context.QueryString[name];
                if(value==null)
                    value = context.FormData[name];
                if (value != null)
                {
                    if (vtype.Name.ToLower().IndexOf("int") >= 0)//int Nullable
                    {
                        var v = Convert.ToInt32(value);
                        Fields[i].SetValue(model, v);
                        continue;
                    }
                    if (vtype.Name.ToLower().IndexOf("nullable") >= 0)//Nullable
                    {
                        var v = Convert.ToDecimal(value);
                        Fields[i].SetValue(model, v);
                        continue;
                    }
                    if (vtype.Name.ToLower().IndexOf("string") >= 0)//string
                    {
                        var v = Convert.ToString(value);
                        Fields[i].SetValue(model, v);
                        continue;
                    }
                    if (vtype.Name.ToLower().IndexOf("datetime") >= 0)//datetime
                    {
                        var v = Convert.ToDateTime(value);
                        Fields[i].SetValue(model, v);
                    }
                    if (vtype.Name.ToLower().IndexOf("bool") >= 0)//datetime
                    {
                        var v = Convert.ToBoolean(value);
                        Fields[i].SetValue(model, v);
                    }
                }
            }
            return model;
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="context"></param>
        /// <param name="key">关键字</param>
        /// <returns></returns>
        public static String GetDataParameter(this IRequest context,string key)
        {
            string result = context.FormData[key];
            if (result == null)
                result = context.QueryString[key];
            return result;
        }
    }
}

 

posted @ 2018-12-28 10:11  扰扰  阅读(197)  评论(0编辑  收藏  举报