标签:type rop soft 声明 home mic href ali image
ylbtech-Code-Convert:public static void LoadModel<T>(T model, Hashtable ht) |
1.返回顶部 |
using System; using System.Collections; namespace Sp.Common { public class ConvertHelper { public static void LoadModel<T>(T model, Hashtable ht) { try { if (model == null) { model = default(T); } Type t = typeof(T); System.Reflection.PropertyInfo[] pros = t.GetProperties(); foreach (var pro in pros) { if (ht.ContainsKey(pro.Name) && null != ht[pro.Name]) { if (pro.PropertyType.Name == "String" && (ht[pro.Name].GetType().Name == "JArray" || ht[pro.Name].GetType().Name == "JObject")) ht[pro.Name] = Newtonsoft.Json.JsonConvert.SerializeObject(ht[pro.Name]); if (pro.PropertyType.IsGenericType && pro.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) pro.SetValue(model, Convert.ChangeType(ht[pro.Name], Nullable.GetUnderlyingType(pro.PropertyType)), null); else if (pro.PropertyType.IsArray) { //pro.SetValue(model,Convert.ChangeType( } else pro.SetValue(model, Convert.ChangeType(ht[pro.Name], pro.PropertyType), null); } } } catch (Exception ex) { throw ex; } } } }
2. 实例返回顶部 |
using System; using System.Collections; using Sp.Common; namespace ConsoleSpLabs { class Program { static void Main(string[] args) { Hashtable hashtable = new Hashtable(); hashtable["Id"] = 101; hashtable["Name"] = "Whale"; Animal dal = new Animal(); ConvertHelper.LoadModel(dal, hashtable); Console.WriteLine("Animal.ID={0},Animal.Name={1}", dal.Id, dal.Name); Console.Read(); } } public class Animal { public int Id { get; set; } public string Name { get; set; } } }
3.返回顶部 |
4.返回顶部 |
5.返回顶部 |
6.返回顶部 |
作者:ylbtech 出处:http://ylbtech.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |
Code-Convert:public static void LoadModel<T>(T model, Hashtable ht)
标签:type rop soft 声明 home mic href ali image
原文地址:https://www.cnblogs.com/storebook/p/12684480.html