C#反射——模仿ParameterInterceptor(ashx处理程序)
反射工具类请参见:https://www.cnblogs.com/threadj/p/10535796.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Collections.Specialized;
using System.Reflection; namespace ReligionServer.util
{
public class ParametersUtil
{ private static NameValueCollection collection = null; //从Request.Param中获取指定的类型字段的值, 并用这些值结合字段初始化一个Hashtable --> hashTable(String fieldName, String fieldName)fieldName来源于Request.Param中
public static Hashtable GetHashtableFormRequest(HttpContext context, Type type)
{ collection = context.Request.Params; Hashtable map = new Hashtable();
//FieldInfo[] fields = ReflectionUtil.GetFileldS(type); //在后来的修改之后, 返回List, 且包含当前类和除开Object的所有父类的属性
String value = "";
foreach (FieldInfo item in ReflectionUtil.GetFileldS(type))
{
value = item.Name.Substring(item.Name.IndexOf("<") + , item.Name.IndexOf(">") - );
map.Add(value, collection.Get(value));
}
//System.Diagnostics.Debug.WriteLine(context.Request.Params.Count);
//ClearForOnlyValueMap(map);//是否有必要
return map;
} /// <summary>
/// 从HttpContext中初始化实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="context"></param>
/// <param name="t"></param>
/// <returns></returns>
public static T GetInstanceFormRequest<T>(HttpContext context, T t)
{
String foreignKey = context.Request["ForeignKey"];
collection = context.Request.Params;
String value = "";
foreach (FieldInfo item in ReflectionUtil.GetFileldS(t.GetType()))
{
if (item.Name.IndexOf("<") != -)
{
value = item.Name.Substring(item.Name.IndexOf("<") + , item.Name.IndexOf(">") - );
}
else
{
value = item.Name;
}
item.SetValue(t, GetValueByType(item.FieldType, collection.Get(value)));
}
return t;
} public static Object GetValueByType(Type type, String value)
{
if (type.Equals(typeof(DateTime)))
{
return DateUtil.ToDateTimeWithFormatf(value);
}
else if (type.Equals(typeof(MongoDB.Bson.BsonValue)))
{
return MongoDB.Bson.BsonValue.Create(value);
}
else if (type.Equals(typeof(Boolean)))
{
value = util.CommonUtil.IsEmpty(value) == true ? "false" : value;//如果上传参数为空, 那么默认初始化为false
return Boolean.Parse(value);
} else if (type.Equals(typeof(int))) {
return Convert.ToInt32(value);
}
return value;
} //将Hashtable中的空值去掉
private static void ClearForOnlyValueMap(Hashtable map)
{
foreach (String key in map)
{
if (null == map[key])
{
map.Remove(key);
}
}
} }
}
C#反射——模仿ParameterInterceptor(ashx处理程序)的更多相关文章
- asp.net ashx处理程序中switch case的替代方案总结
目录 1.用委托字典代替switch...case; 2.利用反射替代switch...case: 3.比较两种方案 4.其他方案 4.说明 5.参考 在开发 asp.net 项目中,通常使用一般处理 ...
- asp.net 登陆后在ashx处理程序中获取不到Session
登录后存储Session,另一个页面Ajax请求 ashx页面,发现无法获取到Session,Session is NULL 使用“IReadOnlySessionState”这个接口就可以
- 关于*.ashx 处理程序调试时 未能创建类型 错误
出现改问题的根本原因是因为,我更改过改类型的名字,而IDE并没有更改 ***.ashx. 注册类型名字. 所更改的类的名字应该是 "项目名字.文件名字“ 例如下图 应该更改类为 ”Wx_ ...
- C#反射——模仿BeanUtil属性复制
反射工具类请参见:https://www.cnblogs.com/threadj/p/10535796.html using System; using System.Collections.Gene ...
- 在ashx处理程序中,如果返回json串数据?
可以通过一下代码: using System.Collections.Generic;using System.Web.Script.Serialization; SortedDictionary&l ...
- 一般处理程序(ashx)和页面处理程序(aspx)的区别
客官请看图 图中的Httphandler就是处理程序. 两者的共同点 如果把aspx处理程序和ashx处理程序放到上图中,他们是处在相同的位置的, 他们都实现了IHttphandler接口.实 ...
- CASE函数 sql server——分组查询(方法和思想) ref和out 一般处理程序结合反射技术统一执行客户端请求 遍历查询结果集,update数据 HBuilder设置APP状态栏
CASE函数 作用: 可以将查询结果集的某一列的字段值进行替换 它可以生成一个新列 相当于switch...case和 if..else 使用语法: case 表达式/字段 when 值 then ...
- C#反射——简单反射操作类的封装
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Re ...
- jQuery post数据至ashx
今天给大家分享一个小功能,在jQuery环境中,Post data to ashx进行数据交互. 参考下面代码示例: $.ajax({ url: '<%= ResolveUrl("~/ ...
随机推荐
- /etc/sysctl.conf
/etc/sysctl.conf 用于控制内核相关的配置参数,而且它的内容全部是对应于 /proc/sys/ 这个目录的子目录及文件 [root@localhost ~]$ ll /proc/sys ...
- linux下用gcc如何生成预处理、汇编等文件
[gcc -E test.c -o test.i------>预处理文件生成.i 文件.] 1.c语言程序生成过程 C语言程序的生成过程可以简单的分为:编辑.预处理.编译.汇编.链接五个阶断. ...
- 安装memcacheq
1.下载memcacheq包 下载地址:http://code.google.com/p/memcacheq/downloads/list 解压包:# tar -zxvf memcache ...
- PyQt4重写事件处理方法
PyQt中的事件处理主要以来重写事件处理函数来实现. #!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 import Qt ...
- JS 获取中英字符串字节长度
正则匹配中文字: 这里限定中文字的范围,一般的使用是没什么问题的.如果要求十分严格的话,那么就只能使用更加严谨的代码匹配了 1:/([^\u0000-\u00FF])/g 2:/[^\x00-\xff ...
- php数据访问之查询关键字
本文根据数据库中的car表做一个汽车查询页面,巩固php查询关键字操作,感兴趣的小伙伴们可以参考一下 本文实例为大家分享了php查询操作的实现代码,供大家参考,具体内容如下 一.一个关键字查询 主 ...
- 神兽保佑-代码无BUG
┏┓ ┏┓┏┛┻━━━┛┻┓┃ ┃ ┃ ━ ┃┃ ┳┛ ┗┳ ┃┃ ┃┃ ┻ ┃┃ ┃┗━┓ ┏━┛ ┃ ┃ 神兽保佑 ┃ ┃ 代码无BUG! ┃ ┗━━━┓ ┃ ┣┓ ...
- #cat ora11g_ora_.trc
Trace file /u02/app/diag/rdbms/ora11g/ora11g/trace/ora11g_ora_31212.trc Oracle Database 11g Enterpri ...
- asymmetric cryptographic algorithm
https://baike.baidu.com/item/非对称加密算法/1208652?fr=aladdin 主要算法 编辑 RSA.Elgamal.背包算法.Rabin.D-H.ECC(椭圆曲线加 ...
- scrapy爬虫系列之一--scrapy的基本用法
功能点:scrapy基本使用 爬取网站:传智播客老师 完整代码:https://files.cnblogs.com/files/bookwed/first.zip 主要代码: ff.py # -*- ...