using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; using System.Threading.Tasks; namespace IOB_UT_NEXT { public class DataExport { /// /// Effettua salvataggio in file di un generico oggetto in formato CSV /// /// /// /// /// Separatore da impiegare (default ";") /// public static async Task SaveToCsv(List reportData, string path, char separator=';') { var lines = new List(); IEnumerable props = TypeDescriptor.GetProperties(typeof(T)).OfType(); var header = string.Join($"{separator}", props.ToList().Select(x => x.Name)); lines.Add(header); var valueLines = reportData.Select(row => string.Join($"{separator}", header.Split(separator).Select(a => row.GetType().GetProperty(a).GetValue(row, null)))); //var valueLines = reportData.Select(row => string.Join(";", header.Split(';').Select(a => row.GetType().GetProperty(a).GetValue(row, null)))); lines.AddRange(valueLines); await Task.Run(() => File.WriteAllLines(path, lines.ToArray())); } } }