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>的更多相关文章

  1. 关于 Dictionary<string,string>,和List<T>在View的使用

    在MVC中Dictionary<string,string>如何应用到View页面中呢,例: <input type="text" name=key value= ...

  2. MVC 自定义IModelBinder实现json参数转Dictionary<string, string>

    IModelBinder的学习不算深入,现在用它来实现一个json转Dictionary<string, string> 一.原始json转Dictionary<string, st ...

  3. C#基础总结之五Dictionary<string, string[]>和while循环

    #region 第五天作业 名片集(01) //Dictionary<string, string[]> PersonCard = new Dictionary<string, st ...

  4. 转:Dictionary<int,string>怎么获取它的值的集合?急!急!急!

    怎么获取Dictionary<int, string>的值?我知道这个是键值对的,我知道可以根据key得到value,但关键是现在连key也不知道啊就是想让这个显示在listbox中,应该 ...

  5. Dictionary<string, string> 排序

    .net framework 2.0 版 Dictionary<string, string> collection = new Dictionary<string, string& ...

  6. C#将URL中的参数转换成字典Dictionary<string, string>

    /// <summary> /// 将获取的formData存入字典数组 /// </summary> public static Dictionary<String, ...

  7. Dictionary<string, string>是一个泛型使用说明

    Dictionary<string, string>是一个泛型使用说明 Posted on 2010-08-05 15:03 moss_tan_jun 阅读(2273) 评论(0) 编辑  ...

  8. Babelfish (关于map<string,string>的用法

    题目链接:https://vjudge.net/contest/237395#problem/A 学习博客:https://blog.csdn.net/lyy289065406/article/det ...

  9. KeyValuePair<string, string>

    ; #region CUP Method /// <summary> /// 请求与响应的超时时间 /// </summary> static public int Timeo ...

随机推荐

  1. python之路---12 生成器 推导式

    三十.函数进阶 1.生成器    函数中有yield 的就是生成器函数(替代了return) 本质就是迭代器   一个一个的创建对象     节省内存 ①创建生成器       最后以yield结束 ...

  2. MongoDB journal与oplog解惑

    journal journal 是 MongoDB 存储引擎层的概念,目前 MongoDB主要支持 mmapv1.wiredtiger.mongorocks 等存储引擎,都支持配置journal. M ...

  3. 新版vue-cli输入本地ip不能访问,只能用localhost才可以访问?

    问:新版vue-cli输入本地ip不能访问,只能用localhost才可以访问? 答:修改config/index.js配置,将host: 'localhost',改为host: '0.0.0.0', ...

  4. IDEA各个版本激活(亲测有效,永久有效)(转)

    之前使用的license server 老是失效,今天又失效了,于是乎,在强大的网上找到了永久激活的方式,有个网站专门提供注册码,但是很这激活码有一定的期限,到期之后再获取一次即可,灰常方便. 激活方 ...

  5. JAVAFX开发桌面应用

    javafx中文版文档: http://www.yiibai.com/javafx/ JavaFX之FXController详解 JavaFx系列教程 含打包部署 javafx之两种局部界面的呈现方式 ...

  6. HDMI初识

    HDMI初识 1.阅读文档xapp1287 (1) KC705 HDMI Reference Design Block Diagram (2) KC705 HDMI Reference Design ...

  7. Xshell配置ssh使用密钥公钥(publice key)登录

    ssh登录提供两种认证方式:口令(密码)认证方式和密钥认证方式.其中口令(密码)认证方式是我们最常用的一种,这里介绍密钥认证方式登录到linux/unix的方法. 使用密钥登录分为3步:1.生成密钥( ...

  8. php 文件读取方式

    整理了一下PHP中读取文件的几个方法,方便以后查阅. 1.fread string fread ( int $handle , int $length ) fread() 从 handle 指向的文件 ...

  9. oracle--合并行数据

    select to_char(wmsys.wm_concat(patname)) as WaitPatientName from (SELECT * FROM (SELECT ROW_NUMBER() ...

  10. 黄聪:如何高效率存储微信中的 access_token

    众所周知,在微信开发中,获取access_token 的接口每天的调用次数是有限制的,2000次应该是. 不过其实这些完全够用了,除非你不小心写了个循环,在1秒中内用完了. 每个access_toke ...