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. redmine和jenkins的ldap登录设置

    工具: softeera LDAP browser 流程: Authentication modes » test Name * Host * Port *  LDAPS Account Passwo ...

  2. 【MatConvNet代码解析】 一些理解

    1.DagNN对象的executionOrder属性 该属性是hidden属性,所以外部是访问不到的.只有dagnn.DagNN类和dagnn.Layer类可以访问到. 该属性的设置是在DagNN类的 ...

  3. Golang cpu的使用设置--GOMAXPROCS

    以下测试,使用的Go版本是1.8.3 不设置 如果没有调用runtime.GOMAXPROCS 去设置CPU,Golang默认使用所有的cpu核. 测试机器CPU有4个核,测试代码开启4个gorout ...

  4. dom实例,模态框,全选,反选,取消

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. awk 文本处理工具

    awk: 强大的文本处理工具,擅长对日志文件进行分析: 不仅用于Linux,也是任何环境中现在的功能最强大的数据处理引擎: 语法说明: awk '{pattern + action}' {filena ...

  6. Release Notes for XE5

    开发者之前说明 http://docwiki.embarcadero.com/RADStudio/XE5/en/Release_Notes_for_XE5

  7. delphi absolute 应用实例

    procedure TForm1.Button1Click(Sender: TObject); var i1,i2:Integer; b:..] of Byte absolute i1; // b 在 ...

  8. jenkins构建触发器详解-不登录触发远程构建

    利用jenkins的远程构建功能,我们可以使用任何脚本,甚至定制一个Web页来控制Job的执行,但是远程构建你如果直接使用的话,老是需要登录才能执行,如何避免登录?稍微折腾了一下,调通了. 1.首先去 ...

  9. C#实现根据日期计算星期

    /// <summary> /// 根据日期返回 星期(返回结果为英文) /// </summary> /// <param name="date"& ...

  10. Azure REST API (4) 在Python环境下,使用Azure REST API

    <Windows Azure Platform 系列文章目录> 之前遇到的项目中,客户需要在Python环境下,监控Azure VM的CPU利用率,在这里简单记录一下. 笔者的环境是Win ...