项目背景:在.net framework下使用asp.net webform,特别是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>
/// StevemChennet@live.com 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>
/// StevemChennet@live.com 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,欢迎同道朋友骚扰。----------------------

代码打包下载(VS2013)

http://files.cnblogs.com/StevenChennet/AppDomainPerformanceDemo.zip

HttpContext的dynamic包装器DynamicHttpContext (附原代码)的更多相关文章

  1. Fms3和Flex打造在线多人视频会议和视频聊天(附原代码)

    Flex,Fms3系列文章导航 Flex,Fms3相关文章索引 本篇是视频聊天,会议开发实例系列文章的第3篇,该系列所有文章链接如下: http://www.cnblogs.com/aierong/a ...

  2. SwiftUI 简明教程之属性包装器

    本文为 Eul 样章,如果您喜欢,请移步 AppStore/Eul 查看更多内容. Eul 是一款 SwiftUI & Combine 教程 App(iOS.macOS),以文章(文字.图片. ...

  3. Java基础(八)对象包装器与自动装箱

    1.对象包装器 有时候,需要将int这样的基本类型转换为对象.所有的基本类型都有一个与之对应的类.通常,这些类被称为包装器(wrapper). 这些对象包装类分别是:Integer.Long.Floa ...

  4. java基础类型、包装器

    char a = 'h';  //类包装器 Character aobj = a ;//自动装箱 byte b = 6; Byte bobj = b; short s = 234; Short sob ...

  5. C++11多态函数对象包装器

    [C++11多态函数对象包装器] 针对函数对象的多态包装器(又称多态函数对象包装器)在语义和语法上和函数指针相似,但不像函数指针那么狭隘.只要能被调用,且其参数能与包装器兼容的都能以多态函数对象包装器 ...

  6. ACE的包装器

    ACE大量运用包装器模式,以期改变面向过程的系统API可视性以及错误处理较难的情况 包装器在若干场合能极大简化代码量,甚至是编码过程 比如ACE_Thread_Mutex 对象创建会自动初始化,很开心 ...

  7. Java中基本数据类型和包装器类型的关系

    在程序设计中经常用到一系列的数据类型,在Java中也一样包含八中数据类型,这八种数据类型又各自对应一种包装器类型.如下表: 基本类型 包装器类型 boolean Boolean char Charac ...

  8. Java 装箱、拆箱 包装器

    先说java的基本数据类型.java基本数据类型:byte.short.int.long.float.double.char.boolean 基本数据类型的自动装箱(autoboxing).拆箱(un ...

  9. c#万能视频播放器(附代码)

    原文:c#万能视频播放器(附代码) c#万能视频播放器 本人之前很多的文章中均提到了使用libvlc为播放器内核制作的播放器,也许有些朋友对此感兴趣,于是我用c#写了一个调用libvlc api实现的 ...

随机推荐

  1. float浮点数的二进制存储方式及转换

    int和float都是4字节32位表示形式.为什么float的范围大于int? float精度为6-7位.1.66*10^10的数字结果并不是166 0000 0000 指数越大,误差越大. 这些问题 ...

  2. 配置项setOption -- title

    标题组件,包含主标题和副标题.在 ECharts 3 中可以存在任意多个标题组件,这在需要标题进行排版,或者单个实例中的多个图表都需要标题时会比较有用. title.show boolean [ de ...

  3. monkeyrunner自动化测试

    通过MonkeyRunner遍历apk文件夹里的apk文件,实现自动安装应用,打开应用,截图,将截图与提前准备好的基线图做对比,对比相似度达到90%时,则对比通过,然后自动卸载应用,进入下一个APK文 ...

  4. 做为一名PHP程序员,应该关注的互联网IT大牛!

    俗话说:“读万卷书不如行万里路,行万里路不如高人指路”,做为一名新人,以后的路还很长,多年你的成就取决于今天的自己付出,选中自己的方向比什么都重要,关注大牛,向他们学习,才不会迷失自己! 1.惠新宸( ...

  5. vue.common.js?e881:433 TypeError: Cannot read property 'nodeName' of undefined

    我觉得吧,是这么个原因,就是响应式要找这个node改它的内容,没找着,就报错了. 用computed监控vuex的state属性,绑定到页面上,如果这个属性改了,因为响应式,那么就要更改页面,如果页面 ...

  6. hihocoder挑战赛26

    某蒟蒻成功的·写出了T1并rank16...小岛的题目真难... 传送门:http://hihocoder.com/contest/challenge26 T1 如果你想要暴力枚举的话显然是不行的 如 ...

  7. flex上下固定中间滚动布局

    传统 pc 端中,子容器高度超出父容器高度,通常使用 overflow:auto 可出现滚动条拖动显示溢出的内容,而移动web开发中,由于浏览器厂商的系统不同.版本不同,导致有部分机型不支持对弹性滚动 ...

  8. Xcode7免证书真机调试

    最近一直忙于项目,对于Xcode7的一些新功能还没去尝试,今天尝试了下挺好用的!避免了以前真机调试繁琐的配置,很是爽啊.又可以节约很多小伙伴的时间了.废话不多说咱们一起来配置一下. 第一步 打开需要真 ...

  9. iOS生成静态库方法

    在iOS的开发过程中,我们常常用到第三方的库.比如支付.地图.广告等. 那么,如何制作自己的库文件呢? 如何将自己写的功能类编译成库文件,分发给其他人来使用呢并做成通用库里? iOS开发一年多来没有制 ...

  10. OC语言中BOOL 和 bool 区别

    1.类型不同 BOOL为int型: bool为布尔型: 2.长度不同 bool只有一个字节: BOOL长度视实际环境来定,一般可认为是4个字节: 3.取值不同 bool取值false和true,是0和 ...