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("~/ ...
随机推荐
- 基于Cocos2d-x学习OpenGL ES 2.0系列——使用VBO索引(4)
在上一篇文章中,我们介绍了uniform和模型-视图-投影变换,相信大家对于OpenGL ES 2.0应该有一点感觉了.在这篇文章中,我们不再画三角形了,改为画四边形.下篇教程,我们就可以画立方体了, ...
- LeetCode——Integer to Roman
Description: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the r ...
- CCNP
CCNP全称是:Cisco Certified Network Professional——思科认证网络高级工程师.CCNP专业人员表示通过认证的人员具有丰富的网络知识.获得CCNP认证的专业人员可以 ...
- nodejs 事件EventEmitter
index.js: // 引入 events 模块 var events = require('events'); //处理函数要写在调用前 var eventHandler = function() ...
- 使用log4net记录日志到数据库(含有自定义属性)
记录日志是管理系统中对用户行为的一种监控与审核,asp.net中记录日志的方式有很多种,这里我只介绍一下最近用到的log4net,关于他的具体介绍网上有很多,我讲一下他的用法. 第一步:在配置文件中的 ...
- C#生成唯一码方法
一.时间戳方法 private string CreateId() { TimeSpan ts = DateTime.UtcNow - , , , , , , ); return Convert.To ...
- reflect 机制
1: Class.forName的作用?为什么要用? 答:调用该访问返回一个以字符串指定类名的类的对象. 2: 通过反射,有几种方法可以实例化Class类对象? 3种,第一种:Class.forNam ...
- phpStorm 显示行号
- PHP unlink()函数,删除文件
定义和用法 unlink() 函数删除文件. 若成功,则返回 true,失败则返回 false. 语法 unlink(filename,context) 参数 描述 filename 必需.规定要删除 ...
- 【转】B2C电子商务系统设计精选
B2C电子商务系统研发——促销引擎设计(一)(Promotion Engine) B2C电子商务系统研发——商品SKU分析和设计(一) B2C电子商务系统研发——商品SKU分析和设计(二) 电商后台系 ...