在一般处理程序中,把Form Post过来的表单集合转换成对象 ,仿 MVC post,反射原理

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using System.Web;
using WebSite.Models;

namespace testWebuploader.Scripts.Plugin.webuploader_v0._1._2
{
    /// <summary>
    /// webUploader 的摘要说明
    /// </summary>
    public class webUploader : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpFileCollection files = context.Request.Files;
            ImgUploadModel imguploadmodel = FormToClass<ImgUploadModel>(context.Request.Form);
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        /// <summary>
        /// 把Form Post过来的表单集合转换成对象 ,仿 MVC post
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <returns></returns>
        public T FormToClass<T>(NameValueCollection collection)
        {
            T t = Activator.CreateInstance<T>();
            PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo _target in properties)
            {
                if (_target.CanWrite)
                {
                    _target.SetValue(t, Convert.ChangeType(collection[_target.Name], _target.PropertyType));
                }
            }
            return t;
        }
    }
}

 

在一般处理程序中,把Form Post过来的表单集合转换成对象 ,仿 MVC post,反射原理,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。