コード例 #1
0
        public static TextPartAttribute[] GetObjectPartAttributes(System.Type t)
        {
            // Using reflection.
            //System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t);  // Reflection.
            List <TextPartAttribute> retour = new List <TextPartAttribute>();

            foreach (System.Reflection.PropertyInfo proper in t.GetProperties())
            {
                foreach (System.Attribute item in proper.GetCustomAttributes(false))
                {
                    if (item is TextPartAttribute)
                    {
                        TextPartAttribute itemfield = (TextPartAttribute)item;
                        itemfield.ObjectPropertyName = proper.Name;
                        itemfield.ObjectPropertyInfo = proper;
                        retour.Add(itemfield);
                    }
                }
            }
            return(retour.ToArray());
        }
コード例 #2
0
ファイル: TextSerializer.cs プロジェクト: NueGy/NgLib
        ///// <summary>
        ///// Enrichir un objet avec des données provenant de la base
        ///// </summary>
        ///// <param name="line"></param>
        ///// <param name="row"></param>
        ///// <returns></returns>
        //public bool ReadDataRow(TexteManipulate line, System.Data.DataRow row)
        //{
        //    bool retour = false;
        //    TextStructure structureline = line.GetStructure();
        //    if (structureline == null) return false;
        //    foreach (TextStructurePart part in structureline.Parts.Values)
        //    {

        //        if (!string.IsNullOrWhiteSpace(part.DataBaseColumn))
        //        {
        //            line[part.Position] = MANIPULATE.DATASET.DataSetTools.GetRowString(row, part.DataBaseColumn);

        //            // !!! ajouter des action/manipulation lors de la lecture en base

        //            retour = true; // on confirme qu'il y as eu des lectures
        //        }
        //    }
        //    return retour;
        //}



        #endregion



        #region Serial Reflexion

        //public List<Tobj> DeSerializationReflexion<Tobj>(params string[] LinesString) where Tobj : new()
        //{
        //    System.Type objType = typeof(Tobj);
        //    List<Tobj> retour = new List<Tobj>();
        //    try
        //    {
        //        GetStructureLineByReflextion(objType);
        //        AttributePart[] attritems = AttributePart.GetObjectPartAttributes(objType);

        //        foreach (string LineString in LinesString)
        //        {
        //            MANIPULATE.TEXT.TextLine textparts = new TextLine(Structures.DefaultStructureLine);
        //            textparts.FillString(LineString);
        //            Tobj lineData = new Tobj();

        //            foreach (AttributePart item in attritems)
        //            {
        //                if (item.Position<0) continue;
        //                MANIPULATE.TEXT.TextPart part = textparts.GetPart(item.Position);
        //                if(part==null) continue;
        //                string resultStr = part.ToString(true, true);
        //                object resultData = ConvertPlus.ChangeType(resultStr, item.ObjectPropertyInfo.PropertyType);
        //                item.ObjectPropertyInfo.SetValue(lineData, resultData,null);
        //            }
        //            retour.Add(lineData);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw new Exception("UnSerialize " + ex.Message, ex);
        //    }

        //    return retour;
        //}



        ///// <summary>
        ///// !!! a developper
        ///// </summary>
        ///// <typeparam name="Tobj"></typeparam>
        ///// <param name="objs"></param>
        ///// <returns></returns>
        //public string[] SerializationReflexion<Tobj>(List<Tobj> objs) where Tobj : new()
        //{
        //    System.Type objType = typeof(Tobj);
        //    //List<string> retour = new List<string>();
        //    List<DATA.MANIPULATE.TEXT.TextLine> texteLines = new List<TextLine>();
        //    try
        //    {


        //        GetStructureLineByReflextion(objType);
        //        AttributePart[] attritems = AttributePart.GetObjectPartAttributes(objType);


        //        foreach (Tobj obj in objs)
        //        {
        //            DATA.MANIPULATE.TEXT.TextLine line = new TextLine(Structures.DefaultStructureLine);

        //            foreach (AttributePart item in attritems)
        //            {
        //                if (item.Position < 0) continue;
        //                object resultData = item.ObjectPropertyInfo.GetValue(obj, null);
        //                string resultString = string.Empty;
        //                if (resultData != null) resultString = Convert.ToString(resultData);
        //                line.SetString(item.Position, resultString);
        //            }
        //            texteLines.Add(line);
        //        }

        //    }
        //    catch (Exception ex)
        //    {
        //        throw new Exception("SerializationReflexion " + ex.Message, ex);
        //    }
        //    return this.SerializationTextLines(texteLines);
        //}



        public TextStructure GetStructureLineByReflextion(System.Type objType)
        {
            try
            {
                TextStructure StructureLineObj = null;

                TextPartAttribute[] attrs    = TextPartAttribute.GetObjectPartAttributes(objType);
                TextLineAttribute   attrline = TextLineAttribute.GetObjectLineAttributes(objType);

                if (attrline == null)
                {
                    return(null);                 //throw new Exception("Attribute line Empty");
                }
                if (string.IsNullOrWhiteSpace(attrline.StructureLineName))
                {
                    return(null);
                }
                if (Structures.StructuresLines.Count == null)
                {
                    Structures.DefaultStructureLine = new TextStructure();
                }

                StructureLineObj = this.Structures.GetStructure(attrline.StructureLineName);
                if (StructureLineObj == null)
                {
                    StructureLineObj = new TextStructure(attrline.StructureLineName);
                }

                StructureLineObj.TextMode = attrline.TextMode;


                // enrichir si la structure existe deja
                foreach (TextPartAttribute item in attrs)
                {
                    TextStructurePart ispart = null;
                    if (!string.IsNullOrWhiteSpace(item.Name))
                    {
                        ispart = StructureLineObj.GetPart(item.Name);                                        // recherche par le nom
                    }
                    else if (item.Position >= 0)
                    {
                        ispart = StructureLineObj.GetPart(item.Position);                          // recherche par la position
                    }
                    if (ispart == null)
                    {
                        if (StructureLineObj.TextMode == TextModeEnum.TXT && item.Length == 0)
                        {
                            continue;                                                                    //inutile ...
                        }
                        ispart          = new TextStructurePart();
                        ispart.Position = item.Position;
                        ispart.Length   = item.Length;
                        ispart.Name     = item.Name;

                        //if (item.StringTransformOptions != null)
                        //    foreach (var item in item.StringTransformOptions)
                        //    {

                        //    }

                        /*
                         * if (!string.IsNullOrWhiteSpace(item.Regex))
                         * {
                         *  if (ispart.data == null) ispart.data = new DATAVALUES.DataValues_data();
                         *  ispart.data["chaine_regex"] = item.Regex;
                         * }*/

                        ispart.CompleteNumber = item.CompleteNumber;
                    }
                    StructureLineObj.SetPart(ispart);
                }


                this.Structures.SetStructure(StructureLineObj);
                return(StructureLineObj);
            }
            catch (Exception ex)
            {
                throw new Exception("GetStructureLineByReflextion " + ex.Message);
            }
        }