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); }
/// <summary> /// 利用反射将ExcelDataRow转换为制定类型,性能较差 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="row"></param> /// <returns></returns> public static T ConvertByRefelection <T>(this ExcelDataRow row) { Type t = typeof(T); object o = Activator.CreateInstance(t); t.GetProperties().ToList().ForEach(p => { if (p.IsDefined(typeof(ColNameAttribute))) { ExcelDataCol col = row.DataCols.SingleOrDefault(c => c.ColName == p.GetCustomAttribute <ColNameAttribute>().ColName); if (col != null) { p.SetValue(o, ExpressionMapper.ChangeType(col.ColValue, p.PropertyType)); } } }); return((T)o); }
/// <summary> /// 利用表达式树,将ExcelDataRow快速转换为指定类型 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="row"></param> /// <returns></returns> public static T FastConvert <T>(this ExcelDataRow row) { return(ExpressionMapper.FastConvert <T>(row)); }