• c# 反射得到实体类的字段名称和值,DataTable转List<T>


    /// <summary>
    /// 反射得到实体类的字段名称和值
    /// var dict = GetProperties(model);
    /// </summary>
    /// <typeparam name="T">实体类</typeparam>
    /// <param name="t">实例化</param>
    /// <returns></returns>
    public static Dictionary<object, object> GetProperties<T>(T t)
    {
    	var ret = new Dictionary<object, object>();
    	if (t == null) { return null; }
    	PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
    	if (properties.Length <= 0) { return null; }
    	foreach (PropertyInfo item in properties)
    	{
    		string name = item.Name;
    		object value = item.GetValue(t, null);
    		if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
    		{
    			ret.Add(name, value);
    		}
    	}
    	return ret;
    }
    
    //通过反射获取实体类 字段名和字段值
    RBAC.Model.SY_ADMIN model = new RBAC.Model.SY_ADMIN();
    var dict = GetProperties(model);
    foreach (var item in dict)
    {
    	str += string.Format("{0}-----{1}<br/>", item.Key, item.Value);
    }
            /*         
                IList<Model1> t1 = DataTableToList<Model1>(dt);         
             */
            /// <summary>
            /// DataTable利用泛型填充实体类
            /// </summary>
            /// <typeparam name="T">实体类</typeparam>
            /// <param name="table">dt</param>
            /// <returns></returns>
            public static IList<T> DataTableToList<T>(DataTable table)
            {
                IList<T> list = new List<T>();
                T t = default(T);
                PropertyInfo[] propertypes = null;
                string tempName = string.Empty;
                foreach (DataRow row in table.Rows)
                {
                    t = Activator.CreateInstance<T>();
                    propertypes = t.GetType().GetProperties();
                    foreach (PropertyInfo pro in propertypes)
                    {
                        tempName = pro.Name;
                        if (table.Columns.Contains(tempName))
                        {
                            object value = MSCL.Until.IsNullOrDBNull(row[tempName]) ? null : row[tempName];
                            pro.SetValue(t, value, null);
                        }
                    }
                    list.Add(t);
                }
                return list;
            }




  • 相关阅读:
    右上角的 那个 个数,
    i am sorry,
    搞定了,一个是重复,一个是 判断navigationcontroller里面 最后的那个类,
    ios no code signature found,
    这次逻辑通了,
    分析下现在 遇到的这个棘手的问题,
    时间 折腾我的,
    老大说 调到 4寸的 就不用适配 5了,
    应用程序生命周期,
    解决【npm ERR! Unexpected end of JSON input while parsing near '...sh_time":141072930277'】方案
  • 原文地址:https://www.cnblogs.com/smartsmile/p/6234049.html
Copyright © 2020-2023  润新知