일반 프로세서에서 Form Post에서 온 폼 집합을 대상으로 변환하여 MVC Post, 반사 원리를 본떠서

4507 단어 form
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;
        }
    }
}

좋은 웹페이지 즐겨찾기