public T[] ReadFile <T>(string fileName) where T : class, new() { string filePath = GetFilePath(fileName); if (!File.Exists(filePath)) { File.Create(filePath).Dispose(); } IFlatFileEngine engine = GetFlatFileEngine <T>(); MethodInfo decorateMethod = engine.GetType().GetMethod("Read"); MethodInfo generic = decorateMethod.MakeGenericMethod(typeof(T)); try { using (var stream = new FileStream(filePath, FileMode.Open)) { var results = generic.Invoke(engine, new object[] { stream }) as IEnumerable <T>; return(results.ToArray()); } } catch (FormatException ex) { Console.WriteLine($"Error while loading file {filePath}, deleting file to ensure it doesn't happen again."); Console.WriteLine(ex); File.Delete(filePath); return(new T[0]); } }
public void SaveFile <T>(string fileName, T[] objects) where T : class, new() { string filePath = GetFilePath(fileName); using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Write)) { IFlatFileEngine engine = GetFlatFileEngine <T>(); MethodInfo decorateMethod = engine.GetType().GetMethod("Write"); MethodInfo generic = decorateMethod.MakeGenericMethod(typeof(T)); generic.Invoke(engine, new object[] { stream, objects }); } }