反射工具类请参见: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);
}
}
} }
}

最新文章

  1. 编写一个基本的连接池来实现连接的复用&amp;一些工程细节上的优化
  2. C++中利用数组对字符进行除重和排序
  3. 用muduo实现memcached协议的例子
  4. Filter Effects - 使用 CSS3 滤镜处理图片
  5. js 操作 字符串
  6. 【LeetCode】168 &amp; 171- Excel Sheet Column Title &amp; Excel Sheet Column Number
  7. 最优秀的5个Linux文本编辑器
  8. Angular初级入门
  9. 算法练习之DP 求LCM (最长公共子序列)
  10. css之outline实现圆角效果
  11. shell编程练习-打印九九乘法表(附:awk编程)
  12. vue实战记录(三)- vue实现购物车功能之渲染商品列表
  13. cf 1041C双指针
  14. Spring Security教程(八):用户认证流程源码详解
  15. JavaScript -- 运算符、eval、Parse
  16. 612.1.003 ALGS4 | Stacks and Queues
  17. grep查看源代码用法
  18. Eclipse酷炫项目、最新趋势介绍
  19. 将std::array转换成std::tuple
  20. (转)一个故事讲完https

热门文章

  1. React如何进行事件传参
  2. Android 按钮 Button和ImageButton
  3. jQuery中的$.each的用法
  4. Zabbix漏洞汇总
  5. 微信小程序 --- 动态获取input的value
  6. springMVC各个包下的作用
  7. SaltStack部署redis主从
  8. opencv学习笔记——颜色空间转换函数cv::cvtColor详解
  9. OpenPGP协议的一个JavaScript实现:OpenPGP.js
  10. MVC项目,bootstrap升级后index.d.ts编译出错