zoukankan      html  css  js  c++  java
  • 在一般处理程序中,把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;
            }
        }
    }
  • 相关阅读:
    ng的ngModel用来处理表单操作
    ionic改tab文字和icon图片的颜色
    ionic安装遇到的一些问题
    ionic运行测试
    安卓sdk安装教程
    ionic教程
    ng 构建
    ng websocket
    ng依赖注入
    Python: 定时器(Timer)简单实现
  • 原文地址:https://www.cnblogs.com/shanhe/p/3733644.html
Copyright © 2011-2022 走看看