/// <summary> /// Loads the data file. /// </summary> /// <param name="filepath">The filepath.</param> /// <param name="output">The output.</param> /// <param name="itemType">Type of the item.</param> /// <returns></returns> /// <exception cref="NotImplementedException"> /// JSON not implemented yet /// or /// binary file not implemented yet /// </exception> /// <exception cref="InvalidDataException">Format type [" + formatMode + "] not supported for type [" + type.Name + "]" + "Format not supported - LoadDataFile</exception> /// <exception cref="System.NotImplementedException">JSON not implemented yet /// or /// binary file not implemented yet</exception> protected Object LoadDataFile(String filepath, ILogBuilder output = null, Type itemType = null) { Object instance = null; if (itemType == null) { itemType = type; } if (File.Exists(filepath)) { if (options.HasFlag(fileDataPropertyOptions.SupportLoadSave)) { ISupportLoadSave loadSave = itemType.getInstance() as ISupportLoadSave; loadSave.name = name; loadSave.LoadFrom(filepath); } else { switch (formatMode) { default: case fileDataPropertyMode.XML: instance = objectSerialization.loadObjectFromXML(filepath, itemType); break; case fileDataPropertyMode.JSON: throw new NotImplementedException("JSON not implemented yet"); break; case fileDataPropertyMode.binary: throw new NotImplementedException("binary file not implemented yet"); break; case fileDataPropertyMode.text: String text = openBase.openFileToString(filepath, false); if (type == typeof(List <String>)) { instance = imbSciStringExtensions.SplitSmart(text, ListStringSeparator, "", false, true); } else if (type == typeof(String)) { instance = text; } else { throw new InvalidDataException("Format type [" + formatMode + "] not supported for type [" + type.Name + "]" + "Format not supported - LoadDataFile"); } break; } } } else { var constructor = type.GetConstructor(new Type[] { }); if (constructor != null) { instance = Activator.CreateInstance(itemType, new Type[] { }); if (output != null) { // output.log("File [" + filepath + "] not found - but new instance of (" + itemType.Name + ") created"); } } else { if (output != null) { //output.log("Loading [" + filepath + "] failed as file not found (" + itemType.Name + ")"); } } } return(instance); }
/// <summary> /// Saves the data file. /// </summary> /// <param name="instance">The instance.</param> /// <param name="fullpath">The fullpath.</param> /// <param name="output">The output.</param> /// <param name="preventThrow">if set to <c>true</c> it will prevent throwing exception.</param> /// <exception cref="System.NotImplementedException">JSON not implemented yet /// or /// binary file not implemented yet</exception> protected void SaveDataFile(Object instance, String fullpath, ILogBuilder output = null, Boolean preventThrow = false) { fullpath = fullpath.getWritableFile(getWritableFileMode.overwrite).FullName; try { if (options.HasFlag(fileDataPropertyOptions.SupportLoadSave)) { ISupportLoadSave loadSave = instance as ISupportLoadSave; loadSave.SaveAs(fullpath, getWritableFileMode.overwrite); } else { switch (formatMode) { case fileDataPropertyMode.XML: objectSerialization.saveObjectToXML(instance, fullpath); break; case fileDataPropertyMode.JSON: throw new NotImplementedException("JSON not implemented yet"); break; case fileDataPropertyMode.binary: throw new NotImplementedException("binary file not implemented yet"); break; case fileDataPropertyMode.text: String text = ""; // openBase.openFileToString(filepath, false); if (type == typeof(List <String>)) { List <String> list = (List <String>)instance; text = list.toCsvInLine(ListStringSeparator); } else if (type == typeof(String)) { text = instance as String; } else { throw new InvalidDataException("Format type [" + formatMode + "] not supported for type [" + type.Name + "] " + "Format not supported - SaveDataFile"); } text.saveStringToFile(fullpath, getWritableFileMode.overwrite); break; } } } catch (Exception ex) { //if (output == null && preventThrow) output = aceLog.loger; fileDataStructureExtensions.FileDataStructureError("Error saving [" + fullpath + "] of type: " + instance.GetType().Name, null, output, ex, instance as IFileDataStructure); if (!preventThrow) { throw; } } }