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. ios Programming:The Big Nerd Ranch Guid(6th Edition) (Joe Conway & AARON HILLEGASS 著)

    Introduction (已看) Prerequisites What Has Changed in the Sixth Edition? Our Teaching Philosophy How t ...

  2. Python网络_TCP/IP简介

    本章将介绍tcp网络编程,更多内容请参考:Python学习指南 Socket是网络编程的一个抽象概念,通常我们用一个Socket表示"打开了一个网络连接",而打开一个Socket需 ...

  3. 谈谈在 .Net 平台上的 软件生态 和 软件生产力

    我们可以先看看这篇文章 : <看 StackOverflow 如何用 25 台服务器撑起 5.6 亿的月 PV>    http://www.nowamagic.net/librarys/ ...

  4. gulp 压缩 js 和 css 代码

    我们在写出来的代码都是非常规范的,改换行的时候就换行,改tab 的时候就有tab,还有这样做是为了后期维护方便,但是这也导致了内存占用量的增大,当把把代码发到线上,如果网速慢一点,可能很久都加载不出来 ...

  5. MySQL 的数据类型,有哪些?

    table th:first-of-type { width: 100px; } MySQL数据类型选择指南:https://www.awaimai.com/1146.html 实数: 数据类型 多少 ...

  6. $.post() 和 $.get() 如何同步请求

    由于$.post() 和 $.get() 默认是 异步请求,如果需要同步请求,则可以进行如下使用: 在$.post()前把ajax设置为同步:$.ajaxSettings.async = false; ...

  7. Tomcat里面的APR配置问题研究

    这里,之所以研究这个问题,是因为我们的生产系统Linux环境下的tomcat日志里面,启动信息的地方有这么一个WARNING. INFO: The APR based Apache Tomcat Na ...

  8. ecstore-安装提示flock,即使绕过检测,安装成功后还是提示t function 解决办法

    安装时先绕过去 安装好后,修改config.php里的TMP_DIR,指向网站目录下的data目录(用绝对路径) 分析:应该是php的运行用户没有对tmp目录的写权限.中电云集的管理工具就出现过

  9. mysql update 忘加 where 文件恢复

    前提条件:mysql :data_row_format=rowmysql> show variables like '%image%';+------------------+-------+| ...

  10. oracle--合并行数据(拼接字符串),获取查询数据的前3条数据...

    --标准函数Lpad 可以实现左补零,但是如果多于需要长度,则会截断字符串 SELECT LPAD ('1' , 3 , '0') FROM DUAL -- return 001 情况一:需要补零.  ...