Ejemplo n.º 1
0
        private static object GetValue <T>(ConvertToListOptions <T> options, KeyValuePair <string, PropertyInfo> columnInfo, DataRow row)
            where T : class, new()
        {
            object val = null;

            if (options.Projection.ContainsKey(columnInfo.Key))
            {
                val = options.Projection[columnInfo.Key].GetMethodInfo().GetParameters().Count() == 1
                    ? options.Projection[columnInfo.Key].DynamicInvoke(row[columnInfo.Key])
                    : options.Projection[columnInfo.Key].DynamicInvoke(row[columnInfo.Key], row);
            }
            else if (columnInfo.Value.PropertyType.IsEnum)
            {
                val = Enum.Parse(columnInfo.Value.PropertyType, row[columnInfo.Key].ToString());
            }
            else
            {
                if (string.IsNullOrWhiteSpace(row[columnInfo.Key].ToString()) && columnInfo.Value.PropertyType.IsNumeric())
                {
                    val = Convert.ChangeType(0, columnInfo.Value.PropertyType);
                }
                else
                {
                    val = Convert.ChangeType(row[columnInfo.Key], columnInfo.Value.PropertyType);
                }
            }
            return(val);
        }
Ejemplo n.º 2
0
        private static void ValidateColumnMapper <T>(DataTable dataTable, ConvertToListOptions <T> options) where T : class
        {
            var requireColumns = options.ColumnMapper.Select(x => x.Key);
            var missColumns    = requireColumns.Where(x => !dataTable.Columns.Contains(x)).ToArray();

            if (missColumns.Count() != 0)
            {
                throw new KeyNotFoundException("缺少列名为:【" + string.Join("】,【", missColumns) + "】的配置信息");
            }
        }
Ejemplo n.º 3
0
 private static void InitOptions <T>(DataConverter <T> dataConverter, ref ConvertToListOptions <T> options) where T : class
 {
     if (options == null)
     {
         options = new ConvertToListOptions <T>();
         foreach (var property in dataConverter.PropertyToShow)
         {
             options.MapColumn(property.Name, property);
         }
     }
 }
Ejemplo n.º 4
0
        private static List <T> Conver <T>(DataConverter <T> dataConverter, DataTable dataTable, ConvertToListOptions <T> options) where T : class, new()
        {
            var lst = new List <T>();

            foreach (DataRow row in dataTable.Rows)
            {
                var item = new T();
                foreach (var columnInfo in options.ColumnMapper)
                {
                    columnInfo.Value.SetValue(item, GetValue(options, columnInfo, row));
                }
                lst.Add(item);
            }
            return(lst);
        }
Ejemplo n.º 5
0
 public static List <T> ToList <T>(this DataConverter <T> dataConverter, DataTable dataTable, ConvertToListOptions <T> options = null) where T : class, new()
 {
     InitOptions(dataConverter, ref options);
     ValidateColumnMapper <T>(dataTable, options);
     return(Conver(dataConverter, dataTable, options));
 }