/// <summary> /// 利用反射将ExcelDataRow转换为制定类型,性能较差 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="row"></param> /// <returns></returns> private static T ConvertByRefelection <T>(this ExcelDataRow row, IEnumerable <PropertyInfo> props = null) { if (props == null) { props = typeof(T).GetProperties(); } object o = Activator.CreateInstance(typeof(T)); props.ToList().ForEach(p => { ExcelDataCol col = null; if (p.IsDefined(typeof(ColNameAttribute))) { col = row.DataCols.SingleOrDefault(c => c.ColName == p.GetCustomAttribute <ColNameAttribute>().ColName); } else { col = row.DataCols.SingleOrDefault(c => c.ColName == p.Name); } if (col != null) { p.SetValue(o, ExpressionMapper.ChangeType(col.ColValue, p.PropertyType)); } }); return((T)o); }
private static object GetValue(ExcelDataRow row, Type propType, string colName) { string val = row.DataCols.SingleOrDefault(c => c.ColName == colName)?.ColValue; if (!string.IsNullOrWhiteSpace(val)) { return(ExpressionMapper.ChangeType(val, propType)); } return(val); }