zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }
  • 相关阅读:
    [设计模式]在CodeDom代码生成中使用Decorator模式实现类型创建
    【翻译】防腐层:面向领域驱动设计的更为稳健的系统集成方案
    EntityFramework之领域驱动设计实践【后续篇】:基于EF 4.3.1 Code First的领域驱动设计实践案例
    Apworks框架中各种仓储实现的性能基准测试与结果对比
    CQRS架构中同步服务的一种实现方式
    在Visual Studio 2010中创建多项目(解决方案)模板【三】
    Microsoft NLayerApp案例理论与实践 应用层
    在Visual Studio 2010中创建多项目(解决方案)模板【二】
    小猫奥斯卡
    测试一下又拍网图片外链
  • 原文地址:https://www.cnblogs.com/raorao1994/p/10188898.html
Copyright © 2011-2022 走看看