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("~/ ...
随机推荐
- 首个vue.js项目收尾中……
前言: 4.26号入手vue.js+elementUI,迄今为止我们的工作应该可以暂时告一段落了:下周开始,又是新的“征程”. 过程:站在接近完成的角度来看这个项目,似乎的确有许多事情需要自己阐述. ...
- poj_2112 网络最大流+二分法
题目大意 有K台挤奶机和C头奶牛,都被视为物体,这K+C个物体之间存在路径.给出一个 (K+C)x(K+C) 的矩阵A,A[i][j]表示物体i和物体j之间的距离,有些物体之间可能没有直接通路. ...
- Android之ListView分页数据加载
1.效果如下: 实例如下: 上图的添加数据按钮可以换成一个进度条 因为没有数据所以我加了一个按钮添加到数据库用于测试:一般在服务器拉去数据需要一定的时间,所以可以弄个进度条来提示用户: 点击加载按 ...
- LeetCode——Best Time to Buy and Sell Stock III
Description: Say you have an array for which the ith element is the price of a given stock on day i. ...
- Android 7.1 SystemUI--Multi-Window多窗口模式
PhoneStatusBar.java private View.OnLongClickListener mRecentsLongClickListener = new View.OnLongClic ...
- vux报错二
执行npm run build后 "build": "node build/build.js", // 输出提示信息 - 提示用户请在 http 服务下查看 ...
- Express 框架的安装
从零开始用 Node.js 实现一个微博系统,功能包括路由控制.页面模板.数据库访问.用户注册.登录.用户会话等内容. Express 框架. MVC 设计模式. ejs 模板引擎 MongoDB 数 ...
- Windows Phone 在读取网络图片之前先显示默认图片
1.新建一个控件WindowsPhoneControl1 WindowsPhoneControl1.xaml <UserControl x:Class="DefaultImage.Wi ...
- java类的成员初始化顺序和初始化块知识
java类的成员初始化顺序和初始化块知识 转自:http://blog.csdn.net/lgfeng218/article/details/7606735 属性.方法.构造方法和自由块都是类中的成员 ...
- angular -- ng-class该如何使用?
ng-class是一个判断是否给某一个元素添加类名的属性: 例如:下面是判断 是否添加 aHover 这个类名: <ul class="nav fl w120 o"> ...