Example #1
0
        ///// <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());
        }
Example #2
0
        /// <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);
        }