QueryString to Dictionary<string, string>
public class ModelConvertHelper<T> where T : new() {// 此处一定要加上new()
public static IList<T> ConvertToModel(DataTable dt) {
IList<T> ts = new List<T>();// 定义集合
Type type = typeof(T); // 获得此模型的类型
string tempName = "";
foreach (DataRow dr in dt.Rows) {
T t = new T();
PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
foreach (PropertyInfo pi in propertys) {
tempName = pi.Name;
if (dt.Columns.Contains(tempName)) {
if (!pi.CanWrite) continue;
object value = dr[tempName];
if (pi.PropertyType.Name == "String") {
pi.SetValue(t, Convert.ToString(value), null);
} else if (pi.PropertyType.Name == "Boolean") {
pi.SetValue(t, Convert.ToBoolean(value), null);
} else if (value != DBNull.Value) {
pi.SetValue(t, value, null);
}
}
}
ts.Add(t);
}
return ts;
}
}
public static void SetProperties<T>(this T source, HttpContext context)
{
var properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public);
var values = HttpContext.Current.Request.QueryString;
foreach (var prop in properties) {
if (values.AllKeys.Contains(prop.Name)) {
var propertyValue = values[prop.Name];
//prop.SetValue(source, propertyValue);
if (!prop.PropertyType.IsGenericType) {
prop.SetValue(source, string.IsNullOrEmpty(propertyValue) ? null : Convert.ChangeType(propertyValue, prop.PropertyType));
} else {
Type genericTypeDefinition = prop.PropertyType.GetGenericTypeDefinition();
if (genericTypeDefinition == typeof(Nullable<>)) {
prop.SetValue(source, string.IsNullOrEmpty(propertyValue) ? null : Convert.ChangeType(propertyValue, Nullable.GetUnderlyingType(prop.PropertyType)));
}
}
}
}
}
mode.SetProperties(HttpContext.Current);
Dictionary<string, string> dict = HttpContext.Current.Request.QueryString.Keys.Cast<string>()
.ToDictionary(k => k, v => HttpContext.Current.Request.QueryString[v]);
QueryString to Dictionary<string, string>的更多相关文章
- 关于 Dictionary<string,string>,和List<T>在View的使用
在MVC中Dictionary<string,string>如何应用到View页面中呢,例: <input type="text" name=key value= ...
- MVC 自定义IModelBinder实现json参数转Dictionary<string, string>
IModelBinder的学习不算深入,现在用它来实现一个json转Dictionary<string, string> 一.原始json转Dictionary<string, st ...
- C#基础总结之五Dictionary<string, string[]>和while循环
#region 第五天作业 名片集(01) //Dictionary<string, string[]> PersonCard = new Dictionary<string, st ...
- 转:Dictionary<int,string>怎么获取它的值的集合?急!急!急!
怎么获取Dictionary<int, string>的值?我知道这个是键值对的,我知道可以根据key得到value,但关键是现在连key也不知道啊就是想让这个显示在listbox中,应该 ...
- Dictionary<string, string> 排序
.net framework 2.0 版 Dictionary<string, string> collection = new Dictionary<string, string& ...
- C#将URL中的参数转换成字典Dictionary<string, string>
/// <summary> /// 将获取的formData存入字典数组 /// </summary> public static Dictionary<String, ...
- Dictionary<string, string>是一个泛型使用说明
Dictionary<string, string>是一个泛型使用说明 Posted on 2010-08-05 15:03 moss_tan_jun 阅读(2273) 评论(0) 编辑 ...
- Babelfish (关于map<string,string>的用法
题目链接:https://vjudge.net/contest/237395#problem/A 学习博客:https://blog.csdn.net/lyy289065406/article/det ...
- KeyValuePair<string, string>
; #region CUP Method /// <summary> /// 请求与响应的超时时间 /// </summary> static public int Timeo ...
随机推荐
- 求Sn=a+aa+aaa+aaaa+aaaaa的前5项之和,其中a是一个数字
思路:所求和为一个数字的前n项和,例如前4项和就是从4+44+444+4444,一直加到第4位,为4个4.所以可以用一个循环来表示每一项的数字,加到前几项就循环几次.然后将每项进行相加就可以求出总和. ...
- mina学习总结
一.简介: Apache Mina Server 是一个网络通信应用框架,Mina 可以帮助我们快速开发高性能.高扩展性的网络通信应用,Mina 提供了事件驱动.异步(Mina 的异步 IO 默认使用 ...
- node api 之:Buffer
在 ECMAScript 2015 引入 TypedArray 之前,JavaScript 语言没有读取或操作二进制数据流的机制. Buffer 类被引入作为 Node.js API 的一部分,使其可 ...
- leftJoin鏈錶查詢
//待使用券碼 $code_record_no = DB::table('fook_platform_order as a') ->select('o.code','o.apportion_bi ...
- django csrf使用教程,解决Forbidden (403)CSRF verification failed. Request aborted.
Django版本号:1.11.15 django中post请求报错:Forbidden (403)CSRF verification failed. Request aborted. HelpReas ...
- Zookeeper命令行world
world:anyone:cdrwa getAcl /imooc/abc 获得节点abc的权限 设置权限为crwa. setAcl /nick/abc world:anyone:crwa 测试删除权 ...
- Linux crontab配置
crontab配置 1.命令功能 通过crontab 命令,我们可以在固定的间隔时间执行指定的系统指令或 shell script脚本.时间间隔的单位可以是分钟.小时.日.月.周及以上的任意组合.这个 ...
- 2.初步认识Angular2
简述:一个完整的Angular应用主要由六个重要部分构成,分别是:组件,模板,指令,服务,依赖注入,和路由.这些组成部分各司其职,而又紧密协作. 其中,与用户直接打交互的是模板视图,它是构成组件的要素 ...
- tomcat端口号被占用的问题
错误原因:8080端口被其他的应用占用! 在网上查了下解决方案,有些说在任务管理窗口关闭javaw.exe,有些说shutdown一下tomcat,亲测在某些场合下可用. 下面是摘自其它博客园里文章的 ...
- php面向对象编程 父类调用子类编程
使用父类调用子类的实现代码