internal void deployDescriptor(Type type, IFileDataStructure dataStructure) { //Type type = dataStructure.GetType(); fileStructureAttribute attribute = Attribute.GetCustomAttribute(type, typeof(fileStructureAttribute)) as fileStructureAttribute; if (attribute != null) { deployDescriptorBase(type, attribute, dataStructure); } var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty); foreach (var prop in properties) { fileDataAttribute propAttribute = Attribute.GetCustomAttribute(prop, typeof(fileDataAttribute)) as fileDataAttribute; if (propAttribute != null) { fileDataPropertyDescriptor propDescriptor = new fileDataPropertyDescriptor(prop, propAttribute, dataStructure); Int32 p = propDescriptor.memberMetaInfo.priority; while (fileDataProperties.ContainsKey(p)) { p++; } fileDataProperties.Add(p, propDescriptor); } } }
/// <summary> /// Saves the data structure: its properties marked with <see cref="fileDataAttribute"/> attribute and it self /// </summary> /// <param name="instance">The instance that has to be saved</param> /// <param name="parentFolder">The parent folder in which this instance will be saved - if not specified the application current folder is used</param> /// <param name="output">Logger</param> /// <returns></returns> /// <exception cref="System.NotImplementedException">Can't have File Data Structure loaded if no file structure mode specified</exception> internal String SaveDataStructure(IFileDataStructure instance, folderNode parentFolder = null, ILogBuilder output = null) { if (parentFolder == null) { parentFolder = new folderNode(); } String desc = instance.description; if (desc.isNullOrEmpty()) { desc = description; } String filename = GetFilenameAndSetInstanceFolder(instance, parentFolder, output); instance.OnBeforeSave(); foreach (var pair in fileDataProperties) { fileDataPropertyDescriptor pDesc = pair.Value; pDesc.SaveData(instance, instance.folder, output); } String filepath = instance.folder.pathFor(filename, getWritableFileMode.overwrite, desc, true); SaveDataFile(instance, filepath, output); return(filepath); }
/// <summary> /// Loads the data structure. /// </summary> /// <param name="path">The path.</param> /// <param name="parentFolder">The parent folder.</param> /// <param name="instance">The instance.</param> /// <param name="output">The output.</param> /// <returns></returns> /// <exception cref="System.NotImplementedException">Can't have File Data Structure loaded if no file structure mode specified</exception> internal IFileDataStructure LoadDataStructure(String path, folderNode parentFolder = null, IFileDataStructure instance = null, ILogBuilder output = null) { if (parentFolder == null) { parentFolder = new folderNode(); } switch (mode) { case fileStructureMode.subdirectory: if (path.Contains(".")) { path = Path.GetDirectoryName(path); if (path.isNullOrEmptyString()) { fileDataStructureExtensions.FileDataStructureError("This is subdirectory data structure, do not use directory names with dot [" + path + "] " + "Path contains dot", parentFolder, output, null, instance); //throw new ArgumentException("This is subdirectory data structure, do not use directory names with dot [" + path + "] " + "Path contains dot", nameof(path)); } } var folder = parentFolder.createDirectory(path, "", false); filename = type.Name.getCleanPropertyName().add(formatMode.GetExtension(), "."); if (instance != null) { instance.folder = folder; } parentFolder = folder; break; case fileStructureMode.none: filename = GetFilepath(path, instance, false); fileDataStructureExtensions.FileDataStructureError("Can't have File Data Structure loaded if no file structure mode specified. " + path, parentFolder, output, null, instance); //throw new NotImplementedException("Can't have File Data Structure loaded if no file structure mode specified"); break; } String filepath = parentFolder.pathFor(filename); if (File.Exists(filepath)) { instance = LoadDataFile(filepath, output) as IFileDataStructure; } else { if (instance == null) { fileDataStructureExtensions.FileDataStructureError("File not found[" + filepath + "] no instance created", parentFolder, output, null, instance); } else { fileDataStructureExtensions.FileDataStructureError("File not found[" + filepath + "] default instance created", parentFolder, output, null, instance); // Console.WriteLine("File not found [" + filepath + "] default instance created"); } } instance.folder = parentFolder; foreach (var pair in fileDataProperties) { fileDataPropertyDescriptor pDesc = pair.Value; try { pDesc.LoadDataAndSet(instance, parentFolder, output); } catch (Exception ex) { fileDataStructureExtensions.FileDataStructureError("Loading property [" + pDesc.name + "] for [" + instance.name + "] failed. " + ex.Message, parentFolder, output, ex, instance); } } if (instance != null) { parentFolder.description = instance.description; parentFolder.caption = instance.name; } try { instance.OnLoaded(); } catch (Exception ex) { fileDataStructureExtensions.FileDataStructureError("instance [" + instance.name + "] OnLoaded() failed. " + ex.Message + " : " + ex.StackTrace, parentFolder, output, ex, instance); //Console.WriteLine("instance [" + instance.name + "] OnLoaded() failed. " + ex.Message + " : " + ex.StackTrace); } return(instance); }