///// <summary> ///// On sérialise des objets en text ///// </summary> ///// <param name="objs"></param> ///// <returns></returns> //public string[] Serialization<Tobj>(Tobj[] objs) where Tobj : TextDatas, new() //{ // List<string> retour = new List<string>(); // foreach (TextDatas obj in objs) // { // var structure = obj.GetSchema(); // TextTools.JoinText(obj, structure); // } // return retour.ToArray(); //} /// <summary> /// On sérialise des objets en text /// </summary> /// <typeparam name="Tobj"></typeparam> /// <param name="objs"></param> /// <returns></returns> public string[] Serialization <Tobj>(Tobj[] objs) where Tobj : new() { List <string> retour = new List <string>(); foreach (Tobj obj in objs) { if (obj is TextDatas) { TextDatas tdata = obj as TextDatas; var structure = tdata.GetSchema(); retour.Add(TextTools.JoinText(tdata, structure)); } else if (obj is IDictionary <int, string> ) // <POSITION,VALEUR> { throw new NotImplementedException(); } else if (obj is IDictionary <string, string> ) // <FIELDNAME,VALEUR> { IDictionary <string, string> tdata = obj as IDictionary <string, string>; throw new NotImplementedException(); } else if (obj is IDictionary <string, object> ) // <FIELDNAME,VALEUR> { IDictionary <string, object> tdata = obj as IDictionary <string, object>; throw new NotImplementedException(); } else { // reflexion } } return(retour.ToArray()); }
///// <summary> ///// Permet de définir une valeur ou si la position est définie créer une structure ///// </summary> ///// <param name="numPart"></param> ///// <param name="value"></param> ///// <param name="lenght"></param> ///// <param name="CreateLegal"></param> ///// <returns></returns> //public bool SetObjectWithPosition(string namepart, string value, int? position = null, int lenght = 0) //{ // try // { // int positionPartWant = GetPosition(namepart); // if (positionPartWant < 0) // modecréation // { // if (!position.HasValue) throw new Exception("position required for create part"); // string realNamepart = null; // if (!FORMAT.StringUtilities.IsNumeric(namepart)) realNamepart = namepart.ToLower().Trim(); // if (this.DataSchema == null) this.DataSchema = new TextStructure(); // TextStructurePart StructurePart = this.DataSchema.SetPart(position.Value, lenght, realNamepart); // Défini la structure du champ // } // return this.SetObject(namepart, value, DataAccessorOptionEnum.None); // } // catch (Exception e) // { // // return false; // throw new Exception(string.Format("SetOrCreateString ({0}) {1}", namepart,e.Message),e); // //throw new Exception("Impossible d'affecter une valeur dans une chaine manipulable(" + numPart + "): " + e.Message); // } //} /// <summary> /// Retourner la chaine de caractère assemblée /// </summary> /// <returns></returns> public override string ToString() { try { return(TextTools.JoinText(this, this.DataSchema)); } catch (Exception) { return(base.ToString()); } }
/// <summary> /// Déserialisation du contenu d'un fichier /// </summary> /// <typeparam name="Tobj"></typeparam> /// <param name="LinesString"></param> /// <param name="StructureName"></param> /// <returns></returns> public List <Tobj> DeSerialization <Tobj>(string[] LinesString, string StructureName = null) where Tobj : new() //IDictionary<int, string> { List <Tobj> retour = new List <Tobj>(); TextStructure structline = null; //bool castTextDatas = false; //if (Tobj is TextDatas) castTextDatas = true; TextLineAttribute attrline = TextLineAttribute.GetObjectLineAttributes(typeof(Tobj)); if (attrline != null && string.IsNullOrWhiteSpace(StructureName)) { StructureName = attrline.StructureLineName; // si non défini en parametre mais présent dans l'attribut } if (!string.IsNullOrWhiteSpace(StructureName)) // si défini attribut ou parametre, sinon sera en auto { structline = Structures.GetStructure(StructureName); if (structline == null) { throw new Exception("structure " + StructureName + " non définie"); } } int iiline = 0; foreach (string linestr in LinesString) { iiline++; if (string.IsNullOrEmpty(linestr)) { continue; } TextStructure structlineitem = structline; try { if (structlineitem == null) { // structlineitem = Structures.FindAuto(linestr); if (structlineitem == null) { structlineitem = Structures.DefaultStructureLine; } if (structlineitem == null) { throw new Exception("structure non définie (Ligne " + iiline + ")"); } } Tobj obj = new Tobj(); IDictionary <string, string> datas = TextTools.SplitText(linestr, structlineitem); if (obj is TextDatas) // on lie l'objet avec la structure { TextDatas tdata = obj as TextDatas; tdata.SetSchema(structlineitem); } else if (obj is IDictionary <int, string> ) // <POSITION,VALEUR> { IDictionary <int, string> tdata = obj as IDictionary <int, string>; } else if (obj is IDictionary <string, string> ) // <FIELDNAME,VALEUR> { IDictionary <string, string> tdata = obj as IDictionary <string, string>; MANIPULATE.COLLECTIONS.CollectionsTools.AddRange <string, string>(tdata, datas, true); } else { //reflexion !!! } retour.Add(obj); } catch (Exception) { if (Safe) { continue; } throw; } } return(retour); }
public TextDatas(TextStructure structure, string OriginalValue) : base(TextTools.SplitText(OriginalValue, structure)) { this.SetSchema(structure); }