zoukankan      html  css  js  c++  java
  • table 转实体

     public class Table2Entity<T> where T : class,new()
        {
            public static List<T> GetEntitys(DataTable dt)
            {
                Dictionary<string, string> columns = new Dictionary<string, string>();
                foreach (DataColumn item in dt.Columns)
                {
                    columns.Add(item.ColumnName.Trim().ToLower(), item.ColumnName);
                }
                List<T> result = new List<T>();
                var proptetis = typeof(T).GetProperties(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
                foreach (DataRow dr in dt.Rows)
                {
                    T obj = new T();
                    foreach (var item in proptetis)
                    {
                        var attributes = item.GetCustomAttributes(typeof(RenameAttribute), true);
                        var columnName = (attributes != null && attributes.Length > 0) ? ((RenameAttribute)(attributes[0])).Name.Trim().ToLower() : item.Name.Trim().ToLower();
                        if (columns.Keys.Contains(columnName))
                        {
                            var mt = item.GetSetMethod(true);
                            var value = dr[columns[columnName]];
                            if (value == null || value == DBNull.Value || string.IsNullOrWhiteSpace(value.ToString())) continue;
                            if (item.PropertyType == typeof(string))
                                mt.Invoke(obj, new object[] { value.ToString() });
                            else if (item.PropertyType == typeof(int) || item.PropertyType == typeof(int?))
                                mt.Invoke(obj, new object[] { Convert.ToInt32(value) });
                            else if (item.PropertyType == typeof(long) || item.PropertyType == typeof(long?))
                                mt.Invoke(obj, new object[] { Convert.ToInt64(value) });
                            else if (item.PropertyType == typeof(double) || item.PropertyType == typeof(double?))
                                mt.Invoke(obj, new object[] { Convert.ToDouble(value) });
                            else if (item.PropertyType == typeof(decimal) || item.PropertyType == typeof(decimal?))
                                mt.Invoke(obj, new object[] { Convert.ToDecimal(value) });
                            else if (item.PropertyType == typeof(DateTime) || item.PropertyType == typeof(DateTime?))
                                mt.Invoke(obj, new object[] { Convert.ToDateTime(value) });
                            else if (item.PropertyType == typeof(float) || item.PropertyType == typeof(float?))
                                mt.Invoke(obj, new object[] { float.Parse(value.ToString()) });
                            else if (item.PropertyType == typeof(bool))
                                mt.Invoke(obj, new object[] { bool.Parse(value.ToString()) });
                            else if (item.PropertyType.BaseType == typeof(System.Enum))
                            {
                                mt.Invoke(obj, new object[] { Convert.ToInt32(value) });
                            }
                        }
                    }
                    result.Add(obj);
                }
                return result;
            }
        }
  • 相关阅读:
    如何学习go源码
    最近打仁王2
    如何在时间复杂度为O(n)空间复杂度为O(1)的情况下完成链表的逆置
    漏洞复现-shellshock-bash破壳
    漏洞复现-aria2-任意文件写入
    漏洞复现-apereo-cas-4.1-rce
    漏洞复现-cgi-httpoxy
    漏洞复现-weblogic_weak_password-getshell
    sqlserver2008安装教程
    mysql内一些可以延时注入的查询语句
  • 原文地址:https://www.cnblogs.com/jasonlwings/p/6808372.html
Copyright © 2011-2022 走看看