private string GetCsvStringFromT(T t) { StringBuilder csvStringBuilder = new StringBuilder(string.Empty); try { var reflectedList = _reflectionHelper.GetReflectedPropertyInfo(t); /*Iterate the column from the Mapper and find the matching columns from the current row and extract only the columns specifed * in mapper and ignore other columns*/ bool firstColumn = true; foreach (string propName in PropertyNameToPersist) { IReflectedPropertyInfo propInfo = reflectedList.FirstOrDefault(i => i.PropertyName.Trim().ToUpper() == propName.Trim().ToUpper()); string columnData = GetCsvColumnDataFromPropertyInfo(propInfo); csvStringBuilder.Append(firstColumn ? columnData : "," + columnData); firstColumn = false; } } catch (Exception ex) { _logger.LogError($"Some error occured in transforming an object into CSV.n{ex.Message}\n{ex.StackTrace}"); LogAndThrowError($"Some error occured in transforming an object into CSV", ErrorCodes.ObjectToCsvConvertionError); } return(csvStringBuilder.ToString() + Environment.NewLine); }
private string GetCsvColumnDataFromPropertyInfo(IReflectedPropertyInfo propInfo) { if ((propInfo.PropertyDataType.ToUpper().Contains("STRING")) || propInfo.PropertyDataType.ToUpper().Contains("DATE")) { return($"\"" + propInfo.PropertyValue.Replace("\"", "~!@") + "\"".Replace("~!@", "\"")); } else { return(propInfo.PropertyValue.Trim()); } }
/// <summary> /// Helper reflection function to extract text csv copatible field from a property /// </summary> /// <param name="propInfo"></param> /// <returns></returns> private string GetCsvColumnDataFromPropertyInfo(IReflectedPropertyInfo propInfo) { if ((propInfo.PropertyDataType.ToUpper().Contains("STRING")) || propInfo.PropertyDataType.ToUpper().Contains("DATE")) { if (propInfo.PropertyValue != null) { //escape double quotes to double, double quotes return($"\"" + propInfo.PropertyValue.Replace("\"", "\"\"") + "\""); } } else { return(propInfo.PropertyValue?.Trim()); } return(string.Empty); }
private string GetCsvStringFromT(T t) { StringBuilder csvStringBuilder = new StringBuilder(string.Empty); var reflectedList = _reflectionHelper.GetReflectedPropertyInfo(t); /*Iterate the column from the Mapper and find the matching columns from the current row and extract only the columns specifed * in mapper and ignore other columns*/ bool firstColumn = true; foreach (string propName in PropertyNameToPersist) { IReflectedPropertyInfo propInfo = reflectedList.FirstOrDefault(i => i.PropertyName.Trim().ToUpper() == propName.Trim().ToUpper()); string columnData = GetCsvColumnDataFromPropertyInfo(propInfo); csvStringBuilder.Append(firstColumn ? columnData : "," + columnData); firstColumn = false; } return(csvStringBuilder.ToString() + Environment.NewLine); }