public T ReadObject(string[] contentline, PropertyInfo[] properties, string[] headerLine) { T obj = new T(); if (properties == null) { Type t = typeof(T); properties = t.GetProperties(); properties = ArrayUtility.ArraySort(properties); } Dictionary <string, string> contentRows = ArrayUtility.MergeTwoArray(headerLine, contentline); foreach (KeyValuePair <string, string> pair in contentRows) { foreach (PropertyInfo property in properties) { if (property.Name.ToLower() == pair.Key.ToLower()) { property.SetValue(obj, pair.Value.Trim()); break; } } } return(obj); }
private static object ReadCSV(string filePath, object classType) { PropertyInfo[] properties = classType.GetType().GetProperties(); ArrayUtility.ArraySort(properties); return(classType); }
public static string ToCsv <T>(string separator, T obj, ref bool blheader) { Type t = typeof(T); //FieldInfo[] fields = t.GetFields(); PropertyInfo[] properties = t.GetProperties(); properties = ArrayUtility.ArraySort(properties); StringBuilder csvdata = new StringBuilder(); //swapnil //if (!blheader) //{ // string header = String.Join(separator, properties.Select(f => f.Name).ToArray()); // csvdata.AppendLine(header); // blheader = true; //} if (!headerAppended) { string header = String.Join(separator, properties.Select(f => f.Name).ToArray()); csvdata.AppendLine(header); headerAppended = true; } csvdata.AppendLine(ToCsvFields(separator, properties, obj)); return(csvdata.ToString().Trim()); }
private static Collection <T> ReadData <T>(string fileName, string delimeter) where T : class, new() { Collection <T> classCollection = new Collection <T>(); CsvReading <T> csvr = new CsvReading <T>(); Type t = typeof(T); //FieldInfo[] fields = t.GetFields(); PropertyInfo[] properties = t.GetProperties(); properties = ArrayUtility.ArraySort(properties); bool firstrow = true; string[] headerrow = null; Hashtable ht = new Hashtable(); using (StreamReader r = new StreamReader(fileName)) { string line; while ((line = r.ReadLine()) != null) { if (firstrow) { string[] delimiterChars = { delimeter }; line = line.Replace("\"", string.Empty).Trim(); headerrow = line.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries); if (!CsVisComaptiblewithClass(properties, headerrow)) { break; } } else { T obj = default(T); string[] delimiterChars = { delimeter }; string[] values = RexCsvSplitter.Split(line); //string[] Contentline = line.Split(delimiterChars, StringSplitOptions.None); obj = csvr.ReadObject(values, properties, headerrow); classCollection.Add(obj); } firstrow = false; } } return(classCollection); }
public static string ToCsv <T>(string separator, List <T> objectlist, ref bool blheader) { Type t = typeof(T); PropertyInfo[] properties = t.GetProperties(); properties = ArrayUtility.ArraySort(properties); StringBuilder csvdata = new StringBuilder(); if (blheader) { string header = String.Join(separator, properties.Select(f => f.Name).ToArray()); csvdata.AppendLine(header); blheader = true; } foreach (T o in objectlist) { csvdata.AppendLine(ToCsvFields(separator, properties, o)); } return(csvdata.ToString().Trim()); }