HttpContextのdynamic包装器DynamicHttpContext(元コード付)

6552 ワード

プロジェクトの背景:Netframeworkでaspを使用する.Netwebform、特にaspx+ajax+ashxでは、ashxバックグラウンドコードが転送パラメータを取得する際に、多くの[...]]が必要で、dynamicで彼をパッケージします.
くだらないことは言わないで、上のコード(文章の一番下にパッケージコードがアップロードされています):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyWebDemo
{
    public partial class UserList : System.Web.UI.Page
    {
        /// <summary>
        /// [email protected] qq:38798579
        /// http://www.cnblogs.com/stevenchennet
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            // 
            //        ,  ["parameter"]   
            //
            //HttpContext context = HttpContext.Current;
            //if (!this.IsPostBack)
            //{
            //    // http://localhost:20000/UserList.aspx?id=1&name=StevenChennet
            //    int id = int.Parse(context.Request.Params["id"]);
            //    
// string name = context.Request.Params["nAMe"]; // this.lbl.InnerText = string.Format("Get id:{0} name:{1}", id, name); //} //else //{ // // form post // int age = int.Parse(context.Request.Form["age"]); // string address = context.Request.Form["AddRESS"]; // this.lbl.InnerText = string.Format("Post age:{0} address:{1}", age, address); //} dynamic dContext = new DynamicHttpContext(HttpContext.Current); if (!this.IsPostBack) { // http://localhost:20000/UserList.aspx?id=1&name=StevenChennet int id = dContext.id; // nAMe Name, , nAMe 。
string name = dContext.nAMe; this.lbl.InnerText = string.Format("Get id:{0} name:{1}", id, name); } else { // form post int age = dContext.Age; string address = dContext.AddRESS; this.lbl.InnerText = string.Format("Post age:{0} address:{1}", age, address); } } } }

  
次は実装コードです
 
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;

namespace System.Web
{
    /// <summary>
    /// [email protected] qq:38798579
    /// http://www.cnblogs.com/stevenchennet
    /// </summary>
    public class DynamicHttpContext : DynamicObject
    {
        private string keyContent;
        private HttpContext httpContext;

        public DynamicHttpContext(HttpContext context)
        {
            this.httpContext = context;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            string key = binder.Name;

            if (httpContext.Request.HttpMethod == "GET")
            {
                this.keyContent = this.httpContext.Request.QueryString.Get(key);
            }
            else
            {
                this.keyContent = this.httpContext.Request.Form.Get(key);
            }

            result = this;
            return true;
        }

        public override bool TryConvert(ConvertBinder binder, out object result)
        {
            result = null;
            Type binderType = binder.Type;

            //int
            if (binderType == typeof(int))
            {
                result = int.Parse(this.keyContent);
            }
            else if (binderType == typeof(int?))
            {
                if (string.IsNullOrWhiteSpace(this.keyContent))
                {
                    result = int.Parse(this.keyContent);
                }
                else
                {
                    result = default(int?);
                }
            }
            // bool
            else if (binderType == typeof(bool))
            {
                result = bool.Parse(this.keyContent);
            }
            else if (binderType == typeof(bool?))
            {
                if (string.IsNullOrWhiteSpace(this.keyContent))
                {
                    result = bool.Parse(this.keyContent);
                }
                else
                {
                    result = default(bool?);
                }
            }
            // datetime
            else if (binderType == typeof(DateTime))
            {
                result = DateTime.Parse(this.keyContent);
            }
            else if (binderType == typeof(DateTime?))
            {
                if (string.IsNullOrWhiteSpace(this.keyContent))
                {
                    result = DateTime.Parse(this.keyContent);
                }
                else
                {
                    result = default(DateTime?);
                }
            }
            // string
            else if (binderType == typeof(string))
            {
                result = this.keyContent;
            }
            else
            {
                throw new NotSupportedException(string.Format("   {0}     ,       ", binderType.ToString()));
            }
            return true;
        }
    }
}
 

 
 
 
------------------------利益関係:.Net軟犬一枚、済南仕事、qq:38798579、同道の友达の嫌がらせを歓迎します.-----------------------
コードパッケージダウンロード(VS 2013)
http://files.cnblogs.com/StevenChennet/AppDomainPerformanceDemo.zip