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 ...
随机推荐
- Ubuntu遇到问题“Could not install packages due to an EnvironmentError: [Errno 13] 权限不够: ”
Ubuntu在使用一些pip的时候会遇到:“Could not install packages due to an EnvironmentError: [Errno 13] 权限不够:”的问题. 在 ...
- Navicat permium工具连接Oracle的配置
目标数据库是Oracle11g R2 64位的 搜索很多说是需要32位的Oracle客户端,而且是要下载 Basic 版本,但是下载32位的Oracle客户端还是不行 最后把Navicat Premi ...
- 从输入URL到页面加载的过程?由一道题完善自己的前端知识体系!
出处:http://mp.weixin.qq.com/s/qMsf4DcMhn2cf0fXC-PLVA 强缓存与弱缓存 缓存可以简单的划分成两种类型: 强缓存( 200fromcache)与 协商缓存 ...
- hbase 相关
----------------------------------------hbase的 安装---------------------------------------- 本地安装: 1 解压 ...
- php格式化数字:位数不足前面加0补足
<?php $var=sprintf("%02d", 2);//生成2位数,不足前面补0 echo $var;//结果为02 ?> 參考:https://blog.cs ...
- Spring Cloud Stream
Spring Cloud Stream是Spring Cloud的组件之一,是一个为微服务应用构建消息驱动能力的框架. 1.导入引用 <dependency> <groupId> ...
- Elasticsearch集成HanLP分词器
1.通过git下载分词器代码. 连接如下:https://gitee.com/hualongdata/hanlp-ext hanlp官网如下:http://hanlp.linrunsoft.com/ ...
- RedHat7安装vmware虚拟机启动报错
提示错误如下: Kernel Headers for version 3.10.0-229.14.1.el7.x86_64 were not found.If you.... 安装kernel dev ...
- spring boot打jar包(maven对jar和lib分离)
spring boot intellij Ide打包有两种方式: 1.maven:熟悉.方便配置灵活 2.Build artifacts:操作比较复杂,jar和lib包分离 重点讲maven如何支持j ...
- 基于tornado的文件上传demo
这里,web框架是tornado的4.0版本,文件上传组件,是用的bootstrap-fileinput. 这个小demo,是给合作伙伴提供的,模拟APP上摄像头拍照,上传给后台服务进行图像识别用,识 ...