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 ...
随机推荐
- AES 加解密 Java实现
package com.smt.cipher.symmetry; import java.nio.charset.Charset; import java.security.SecureRandom; ...
- thinkphp5 怎么获取当前的模块,控制器和方法名
//当前模块url地址 $request= Request::instance(); $module_name=$request->module(); $controller_name=$req ...
- tornado输入-get_query_argument()等 笔记
最外面的代码结构 import tornado.web import tornado.ioloop import tornado.options import tornado.httpserver f ...
- python kafka client--confluent-kafka-python
项目中需要使用python 向Kafka生产和消费数据,最初使用pykafka .后来发现pykafka不支持client.id. 最后,终于找到confluent-kafka. python kaf ...
- mac里安装Mycrypt扩展
https://jingyan.baidu.com/article/e3c78d644cf1ed3c4c85f5a8.html 先用homebrew安装mycrpt 再下载php5.6版本源码 然后进 ...
- ecstore-ftp设置,不能上传文件
某些主机居然不能上传,ftp改成127.0.0.1即可 ftp地址改成127.0.0.1即可...
- 《JavaScript设计模式与开发》笔记 2.this指针
1.作为对象方法调用 2.作为普通函数调用 1.作为普通函数进行调用 2.将函数赋值给一个对象 3.callback内部的this指向了window 4.解决callback内部的this问题 3.构 ...
- php函数总结2
1.匿名参数 func_get_args //获取参数列表的数组 func_get_arg(num) //数组索引 func_num_args() //获取匿名函数的个数 <?php funct ...
- Azure PowerShell (16) 并行开关机Azure ARM VM
<Windows Azure Platform 系列文章目录> 并行开机脚本: https://github.com/leizhang1984/AzureChinaPowerShell/b ...
- 黄聪:多个wordpress网站(不同域名)共享用户数据的方法
WordPress可以自定义用户数据表,这样多个wordpress网站就可以共享用户数据了,有时候这是非常方便的,这些Wordpress站点应该安装在同一个数据库下,数据表前缀各不相同.由于Wordp ...