/// <summary> /// プロパティを指定したラムダ式からプロパティチェインの情報を取得。 /// </summary> /// <param name="expression"></param> /// <returns></returns> public static IDictionary <string, PropertyDescriptor> GetPropertyChain(LambdaExpression expression) { var result = new Dictionary <string, PropertyDescriptor>(); var memberExpression = ExpressionUtil.GetMemberExpression(expression); while (memberExpression != null) { var properties = TypeDescriptor.GetProperties(memberExpression.Member.DeclaringType); result.Add(memberExpression.Member.Name, properties.Find(memberExpression.Member.Name, false)); memberExpression = memberExpression.Expression as MemberExpression; } return(result); }
/// <summary> /// 設定されているカラム情報を使って、結果オブジェクトを作成。 /// </summary> /// <param name="headers"></param> /// <param name="lines"></param> /// <returns></returns> IEnumerable <T> CreateObjects(IEnumerable <string> headers, IList <IEnumerable <string> > lines) { headers = headers ?? this.CreateHeaders(); var result = new List <T>(); foreach (var values in lines) { var item = new T(); for (var i = 0; i < headers.Count(); i++) { var header = headers.ElementAt(i); var column = this._columns.Where(x => x.ColumnName == header).SingleOrDefault(); if (column == null) { continue; } var value = column.IsConstantValue ? column.ConstantValue : values.ElementAt(i); if (string.IsNullOrEmpty(value)) { continue; } object current = item; var propertyChain = ExpressionUtil.GetPropertyChain(column.Property).Reverse(); for (var j = 0; j < propertyChain.Count() - 1; j++) { var chainItem = propertyChain.ElementAt(j); var cValue = chainItem.Value.GetValue(current) ?? Activator.CreateInstance(chainItem.Value.PropertyType); chainItem.Value.SetValue(current, cValue); current = cValue; } var lastItem = propertyChain.Last(); if (column.Converter != null) { lastItem.Value.SetValue(current, column.Converter.CsvToObject(value)); } else { lastItem.Value.SetValue(current, lastItem.Value.Converter.ConvertFrom(value)); } } result.Add(item); } return(result); }
/// <summary> /// CSVファイルにエクスポート。 /// </summary> /// <param name="source">エクスポートを行うオブジェクトのリスト</param> /// <param name="filePath">ファイルパス</param> /// <param name="encoding">ファイルのエンコード(既定はUTF-8)</param> public void Export(IEnumerable <T> source, string filePath, Encoding encoding = null) { var result = new List <string>(); var delimiter = string.IsNullOrEmpty(this.Delimiter) ? DEFAULT_DELIMITER : this.Delimiter; if (this.IsContainHeader) { result.Add(this.CreateHeader(delimiter)); } foreach (var item in source) { var values = new List <string>(); foreach (var column in this._columns) { if (column.IsConstantValue) { values.Add(column.ConstantValue); } else { var propertyChain = ExpressionUtil.GetPropertyChain(column.Property); object value = item; foreach (var chainItem in propertyChain.Reverse()) { value = chainItem.Value.GetValue(value); } if (column.Converter != null) { values.Add(column.Converter.ObjectToCsv(value)); } else { values.Add(value != null ? value.ToString() : string.Empty); } } } result.Add(string.Join(delimiter, values)); } encoding = encoding ?? Encoding.UTF8; File.WriteAllLines(filePath, result, encoding); }