Exemplo n.º 1
0
        private static void WriteObjectType(StreamWriter streamWriter, object objectType)
        {
            EdiSegmentAttribute EdiSegmentAttributeMaster = (EdiSegmentAttribute)objectType.GetType().GetCustomAttribute(typeof(EdiSegmentAttribute));

            if (EdiSegmentAttributeMaster == null)
            {
                Type           master = objectType.GetType();
                PropertyInfo[] propertyInfoMasters = master.GetProperties();
                foreach (PropertyInfo propertyInfoMaster in propertyInfoMasters)
                {
                    object objectTypeDetail = propertyInfoMaster.GetValue(objectType);
                    if (objectTypeDetail == null)
                    {
                        continue;
                    }
                    EdiSegmentAttribute ediSegmentAttribute = (EdiSegmentAttribute)objectTypeDetail.GetType().GetCustomAttribute(typeof(EdiSegmentAttribute));
                    if (ediSegmentAttribute != null)
                    {
                        SetEdiSegmentAttribute(streamWriter, objectTypeDetail, ediSegmentAttribute);
                    }
                    else
                    {
                        IList collections = (IList)objectTypeDetail;
                        foreach (object collection in collections)
                        {
                            WriteObjectType(streamWriter, collection);
                        }
                    }
                }
            }
            else
            {
                SetEdiSegmentAttribute(streamWriter, objectType, EdiSegmentAttributeMaster);
            }
        }
Exemplo n.º 2
0
        public static string[] SetPropertyInfo(string[] ReadToEndLines, object dataObject, PropertyInfo propertyInfo, Type propertyType)
        {
            EdiSegmentAttribute eEdiSegmentAttribute = (EdiSegmentAttribute)propertyType.GetCustomAttribute(typeof(EdiSegmentAttribute));

            foreach (string readToEndLine in ReadToEndLines)
            {
                string[] parts  = readToEndLine.Split('*');
                string   master = parts[0];
                if (eEdiSegmentAttribute != null)
                {
                    if (eEdiSegmentAttribute.Path == master)
                    {
                        object IsClassObject = Activator.CreateInstance(propertyType);
                        SetPropertyInfo(parts, IsClassObject);
                        propertyInfo.SetValue(dataObject, IsClassObject);
                        ReadToEndLines = ReadToEndLines.Where(w => w != readToEndLine).ToArray();
                        break;
                    }
                }
            }
            return(ReadToEndLines);
        }
        public static string[] IsNotGenericType(string[] readToEndLines, object dataObject)
        {
            if (readToEndLines.Length == 0)
            {
                return(readToEndLines);
            }
            Type dataObjectType = dataObject.GetType();

            PropertyInfo[] propertyInfos = dataObjectType.GetProperties();
            try {
                propertyInfos = dataObjectType.GetProperties().OrderBy(p => p.GetCustomAttributes().OfType <EdiSegmentAttribute>().First().Order).ToArray();
            }
            catch {
                propertyInfos = dataObjectType.GetProperties().OrderBy(p => p.GetCustomAttributes().OfType <EdiOrderAttribute>().First().Order).ToArray();
            }
            for (int i = 0; i < propertyInfos.Length; i++)
            {
                bool IsHave       = true;
                Type propertyType = propertyInfos[i].PropertyType;
                EdiSegmentAttribute eEdiSegmentAttribute = (EdiSegmentAttribute)propertyInfos[i].GetCustomAttribute(typeof(EdiSegmentAttribute));
                if ((eEdiSegmentAttribute != null) && (eEdiSegmentAttribute.SequenceEnd != null))
                {
                    if (!propertyType.IsGenericType)
                    {
                        EdiSegmentAttribute eEdiSegmentAttributeproperty = (EdiSegmentAttribute)propertyType.GetCustomAttribute(typeof(EdiSegmentAttribute));
                        if (eEdiSegmentAttributeproperty != null)
                        {
                            IsHave = false;
                            foreach (string item in readToEndLines)
                            {
                                if (item.StartsWith(eEdiSegmentAttribute.SequenceEnd))
                                {
                                    break;
                                }
                                else if (item.StartsWith(eEdiSegmentAttributeproperty.Path))
                                {
                                    IsHave = true;
                                }
                            }
                        }
                    }
                }
                else
                {
                    EdiValueAttribute ediValueAttribute = (EdiValueAttribute)propertyInfos[i].GetCustomAttribute(typeof(EdiValueAttribute));
                    if (ediValueAttribute != null)
                    {
                        if (ediValueAttribute != null)
                        {
                            if (readToEndLines.Length > 0)
                            {
                                string[] parts = readToEndLines[0].Split('+');
                                if (ediValueAttribute.Path == parts[0])
                                {
                                    parts = parts.Skip(1).ToArray();
                                    if (parts.Length == 1)
                                    {
                                        SetPropertyInfo(parts[0].Split(':'), dataObject);
                                        readToEndLines = readToEndLines.Where(w => w != readToEndLines[0]).ToArray();
                                    }
                                    else
                                    {
                                        SetPropertyInfo(parts, dataObject);
                                        readToEndLines = readToEndLines.Where(w => w != readToEndLines[0]).ToArray();
                                    }
                                }
                            }
                        }
                    }
                }
                if (!propertyType.IsGenericType && IsHave)
                {
                    if (readToEndLines.Length > 0)
                    {
                        readToEndLines = SetPropertyInfo(readToEndLines, dataObject, propertyInfos[i], propertyType);
                    }
                }
            }
            return(readToEndLines);
        }
Exemplo n.º 4
0
        public static string[] IsGenericType(string[] readToEndLines, object dataObject)
        {
            Type dataObjectType = dataObject.GetType();

            PropertyInfo[] propertyInfos = dataObjectType.GetProperties();
            try {
                propertyInfos = dataObjectType.GetProperties().OrderBy(p => p.GetCustomAttributes().OfType <EdiSegmentAttribute>().First().Order).ToArray();
            }
            catch {
                propertyInfos = dataObjectType.GetProperties().OrderBy(p => p.GetCustomAttributes().OfType <EdiValueAttribute>().First().Order).ToArray();
            }

            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                Type propertyType = propertyInfo.PropertyType;
                if (propertyType.IsGenericType)
                {
                    EdiSegmentAttribute eEdiSegmentAttribute = (EdiSegmentAttribute)propertyInfo.GetCustomAttribute(typeof(EdiSegmentAttribute));
                    Type            GenericArgument          = propertyInfo.PropertyType.GetGenericArguments()[0];
                    List <string[]> bloks    = new List <string[]>();
                    List <string>   tempBlok = new List <string>();
                    foreach (string readToEndLine in readToEndLines)
                    {
                        if (eEdiSegmentAttribute.SequenceEnd == null)
                        {
                        }
                        else
                        {
                            if ((tempBlok.Count > 0) && (readToEndLine.StartsWith(eEdiSegmentAttribute.SequenceEnd) | readToEndLine.StartsWith(eEdiSegmentAttribute.Path)))
                            {
                                if (eEdiSegmentAttribute.IsWithSequenceEnd)
                                {
                                    tempBlok.Add(readToEndLine);
                                    bloks.Add(tempBlok.ToArray());
                                    tempBlok.Clear();
                                }
                                else
                                {
                                    bloks.Add(tempBlok.ToArray());
                                    //readToEndLines = readToEndLines.Skip(tempBlok.Count).ToArray();
                                    tempBlok.Clear();
                                    tempBlok.Add(readToEndLine);
                                }
                            }
                            else
                            {
                                tempBlok.Add(readToEndLine);
                            }
                        }
                    }
                    if (tempBlok.Count > 0)
                    {
                        bloks.Add(tempBlok.ToArray());
                        tempBlok.Clear();
                    }
                    object instance = Activator.CreateInstance(propertyInfo.PropertyType);
                    IList  list     = (IList)instance;
                    if (bloks.Count == 0)
                    {
                        bool           IsValueType         = true;
                        PropertyInfo[] propertiesForValues = GenericArgument.GetProperties();
                        foreach (PropertyInfo propertiesForValue in propertiesForValues)
                        {
                            IsValueType &= propertiesForValue.GetCustomAttribute <EdiValueAttribute>() != null;
                        }

                        if (!IsValueType)
                        {
                            object newObject = Edi850Deserialize.DeserializeInternal(readToEndLines, GenericArgument);
                            list.Add(newObject);
                        }
                        else
                        {
                            foreach (PropertyInfo propertiesForValue in propertiesForValues)
                            {
                                if (readToEndLines.Length > 0)
                                {
                                    EdiValueAttribute EdiValueAttributeValue = propertiesForValue.GetCustomAttribute <EdiValueAttribute>();
                                    string[]          ValueResults           = readToEndLines.Where(x => x.Contains(EdiValueAttributeValue.Path)).ToArray();
                                    foreach (string ValueResult in ValueResults)
                                    {
                                        object newObject = Edi850Deserialize.DeserializeInternal(new[] { ValueResult }, GenericArgument);
                                        list.Add(newObject);
                                        readToEndLines = readToEndLines.Where(w => w != ValueResult).ToArray();
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (string[] items in bloks)
                        {
                            if (items[0].StartsWith(eEdiSegmentAttribute.Path))
                            {
                                object newObject = Edi850Deserialize.DeserializeInternal(items, GenericArgument);
                                list.Add(newObject);
                            }
                        }
                    }
                    propertyInfo.SetValue(dataObject, list, null);
                }
            }
            return(readToEndLines);
        }
Exemplo n.º 5
0
        private object DeserializeInternal(string[] ReadToEndLines, Type objectType)
        {
            object dataObject     = Activator.CreateInstance(objectType);
            Type   dataObjectType = dataObject.GetType();

            PropertyInfo[] propertyInfos = dataObjectType.GetProperties();

            EdiValueAttribute ediValueAttribute = (EdiValueAttribute)propertyInfos[0].GetCustomAttribute(typeof(EdiValueAttribute));

            if (ediValueAttribute != null)
            {
                var      a     = objectType.GetCustomAttributes();
                string[] parts = ReadToEndLines[0].Split('*');
                SetPropertyInfo(parts, dataObject);
                return(dataObject);
            }
            propertyInfos = dataObjectType.GetProperties().OrderBy(p => p.GetCustomAttributes().OfType <EdiSegmentAttribute>().First().Order).ToArray();
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                Type propertyType = propertyInfo.PropertyType;
                if (!propertyType.IsGenericType)
                {
                    ReadToEndLines = SetPropertyInfo(ReadToEndLines, dataObject, propertyInfo, propertyType);
                }
            }

            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                Type propertyType = propertyInfo.PropertyType;
                if (propertyType.IsGenericType)
                {
                    EdiSegmentAttribute eEdiSegmentAttribute = (EdiSegmentAttribute)propertyInfo.GetCustomAttribute(typeof(EdiSegmentAttribute));
                    Type GenericArgument = propertyInfo.PropertyType.GetGenericArguments()[0];

                    List <string>   readToEndLineTemp = new List <string>();
                    List <string[]> details           = new List <string[]>();
                    foreach (string readToEndLine in ReadToEndLines)
                    {
                        string[] parts  = readToEndLine.Split('*');
                        string   master = parts[0];
                        if (eEdiSegmentAttribute.SequenceEnd == null)
                        {
                            IEnumerable <Attribute> Attributes = GenericArgument.GetCustomAttributes();
                            if (Attributes.Count() == 0)
                            {
                                readToEndLineTemp.Add(readToEndLine);
                            }
                            else
                            {
                                if (eEdiSegmentAttribute.Path == master)
                                {
                                    readToEndLineTemp.Add(readToEndLine);
                                }
                            }
                        }
                        else if (eEdiSegmentAttribute.SequenceEnd == master)
                        {
                            readToEndLineTemp.Add(readToEndLine);
                            details.Add(readToEndLineTemp.ToArray());
                            readToEndLineTemp = new List <string>();
                        }
                        else
                        {
                            readToEndLineTemp.Add(readToEndLine);
                        }
                    }
                    if (details.Count > 0)
                    {
                        object instance = Activator.CreateInstance(propertyInfo.PropertyType);
                        IList  list     = (IList)instance;
                        foreach (string[] strings in details)
                        {
                            object newObject = DeserializeInternal(strings, GenericArgument);
                            list.Add(newObject);
                        }
                        propertyInfo.SetValue(dataObject, list, null);
                    }
                    else
                    {
                        List <string> readToEndLineTempDetay = new List <string>();
                        foreach (string readToEndLine in readToEndLineTemp)
                        {
                            string[] parts  = readToEndLine.Split('*');
                            string   master = parts[0];
                            if (master == "SE")
                            {
                                continue;
                            }
                            if ((eEdiSegmentAttribute.Path == master) && (readToEndLineTempDetay.Count > 1))
                            {
                                details.Add(readToEndLineTempDetay.ToArray());
                                readToEndLineTempDetay = new List <string>();
                            }
                            readToEndLineTempDetay.Add(readToEndLine);
                        }
                        if (readToEndLineTempDetay.Count > 0)
                        {
                            details.Add(readToEndLineTempDetay.ToArray());
                            readToEndLineTempDetay = new List <string>();
                        }

                        object instance = Activator.CreateInstance(propertyInfo.PropertyType);
                        IList  list     = (IList)instance;
                        foreach (string[] strings in details)
                        {
                            object newObject = DeserializeInternal(strings, GenericArgument);
                            list.Add(newObject);
                        }
                        propertyInfo.SetValue(dataObject, list, null);
                    }
                }
            }
            return(dataObject);
        }
Exemplo n.º 6
0
        private object DeserializeEdi990(string[] ReadToEndLines, Type objectType)
        {
            #region Edi Validation

            /*
             * if (ReadToEndLines.Length < 4)
             *  throw new Exception("Edi hatalı!");
             *
             * string ISALine = ReadToEndLines[0];
             * string[] ISALineParts = ISALine.Split('*');
             * if (ISALineParts[0] != "ISA")
             *  throw new Exception("Edi dosyası ISA ile başlamalıdır!");
             *
             * string GSLine = ReadToEndLines[1];
             * string[] GSLineParts = GSLine.Split('*');
             * if (GSLineParts[0] != "GS")
             *  throw new Exception("Edi dosyası 2. satırda GS içermelidir!");
             *
             * string GELine = ReadToEndLines[ReadToEndLines.Length - 2];
             * string[] GELineParts = GELine.Split('*');
             * if (GELineParts[0] != "GE")
             *  throw new Exception("Edi dosyası GE bilgisi içermelidir!");
             *
             * string IEALine = ReadToEndLines[ReadToEndLines.Length - 1];
             * string[] IEALineParts = IEALine.Split('*');
             * if (IEALineParts[0] != "IEA")
             *  throw new Exception("Edi dosyası IEA ile bitmelidir!");
             */

            object dataObject     = Activator.CreateInstance(objectType);
            Type   dataObjectType = dataObject.GetType();


            foreach (PropertyInfo propertyInfo in dataObjectType.GetProperties())
            {
                Type propertyType = propertyInfo.PropertyType;
                if (!propertyType.IsGenericType)
                {
                    ReadToEndLines = SetPropertyInfo(ReadToEndLines, dataObject, propertyInfo, propertyType);
                }
            }
            #endregion Edi Validation

            PropertyInfo[] propertyInfos = dataObjectType.GetProperties();
            propertyInfos = dataObjectType.GetProperties().OrderBy(p => p.GetCustomAttributes().OfType <EdiSegmentAttribute>().First().Order).ToArray();
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                Type propertyType = propertyInfo.PropertyType;
                if (propertyType.IsGenericType)
                {
                    EdiSegmentAttribute eEdiSegmentAttribute = (EdiSegmentAttribute)propertyInfo.GetCustomAttribute(typeof(EdiSegmentAttribute));
                    Type GenericArgument = propertyInfo.PropertyType.GetGenericArguments()[0];

                    List <string>   readToEndLineTemp = new List <string>();
                    List <string[]> details           = new List <string[]>();
                    foreach (string readToEndLine in ReadToEndLines)
                    {
                        string[] parts  = readToEndLine.Split('*');
                        string   master = parts[0];
                        if (eEdiSegmentAttribute.SequenceEnd == null)
                        {
                            IEnumerable <Attribute> Attributes = GenericArgument.GetCustomAttributes();
                            if (Attributes.Count() == 0)
                            {
                                readToEndLineTemp.Add(readToEndLine);
                            }
                            else
                            {
                                if (eEdiSegmentAttribute.Path == master)
                                {
                                    readToEndLineTemp.Add(readToEndLine);
                                }
                            }
                        }
                        else if (eEdiSegmentAttribute.SequenceEnd == master)
                        {
                            readToEndLineTemp.Add(readToEndLine);
                            details.Add(readToEndLineTemp.ToArray());
                            readToEndLineTemp = new List <string>();
                        }
                        else
                        {
                            readToEndLineTemp.Add(readToEndLine);
                        }
                    }
                    if (details.Count > 0)
                    {
                        object instance = Activator.CreateInstance(propertyInfo.PropertyType);
                        IList  list     = (IList)instance;
                        foreach (string[] strings in details)
                        {
                            object newObject = DeserializeInternal(strings, GenericArgument);
                            list.Add(newObject);
                        }
                        propertyInfo.SetValue(dataObject, list, null);
                    }
                    else
                    {
                        List <string> readToEndLineTempDetay = new List <string>();
                        foreach (string readToEndLine in readToEndLineTemp)
                        {
                            string[] parts  = readToEndLine.Split('*');
                            string   master = parts[0];
                            if ((eEdiSegmentAttribute.Path == master) && (readToEndLineTempDetay.Count > 1))
                            {
                                details.Add(readToEndLineTempDetay.ToArray());
                                readToEndLineTempDetay = new List <string>();
                            }
                            readToEndLineTempDetay.Add(readToEndLine);
                        }
                        if (readToEndLineTempDetay.Count > 0)
                        {
                            details.Add(readToEndLineTempDetay.ToArray());
                            readToEndLineTempDetay = new List <string>();
                        }

                        object instance = Activator.CreateInstance(propertyInfo.PropertyType);
                        IList  list     = (IList)instance;
                        foreach (string[] strings in details)
                        {
                            IEnumerable <Attribute> Attributes = GenericArgument.GetCustomAttributes();
                            if (Attributes.Count() == 0)
                            {
                                object newObject = DeserializeInternal(strings, GenericArgument);
                                list.Add(newObject);
                            }
                            else
                            {
                                foreach (string item in strings)
                                {
                                    object newObject = DeserializeInternal(new string[] { item }, GenericArgument);
                                    list.Add(newObject);
                                }
                            }
                        }
                        propertyInfo.SetValue(dataObject, list, null);
                    }
                }
            }
            return(dataObject);
        }
Exemplo n.º 7
0
        public object DeserializeInternal(ref string[] ReadToEndLines, Type objectType)
        {
            object dataObject = Activator.CreateInstance(objectType);

            PropertyInfo[] propertyInfos = PropertyClass.GetPropertyInfos(dataObject.GetType().GetProperties(), typeof(EdiSegmentAttribute));
            if (propertyInfos.Length == 0)
            {
                propertyInfos = PropertyClass.GetPropertyInfos(dataObject.GetType().GetProperties(), typeof(EdiOrderAttribute));
                for (int i = 0; i < propertyInfos.Length; i++)
                {
                    PropertyInfo propertyInfo = propertyInfos[i];
                    if (propertyInfo.PropertyType == typeof(string))
                    {
                        var a = ReadToEndLines[0].Split('+');
                        if (a.Length > i)
                        {
                            propertyInfo.SetValue(dataObject, a[i]);
                        }
                    }
                    else
                    {
                        object         dataObject2        = Activator.CreateInstance(propertyInfo.PropertyType);
                        PropertyInfo[] PropertyInfoIsType = PropertyClass.GetPropertyInfos(dataObject2.GetType().GetProperties(), typeof(EdiOrderAttribute));
                        IsType(ReadToEndLines[0].Split('+')[1].Split(':'), dataObject2, PropertyInfoIsType);
                        propertyInfo.SetValue(dataObject, dataObject2, null);
                        ReadToEndLines = ReadToEndLines.Skip(1).ToArray();
                    }
                }
            }
            else
            {
                foreach (PropertyInfo propertyInfo in propertyInfos)
                {
                    if (propertyInfo.PropertyType.IsGenericType)
                    {
                        object dataObject2     = Activator.CreateInstance(propertyInfo.PropertyType);
                        IList  list            = (IList)dataObject2;
                        Type   GenericArgument = propertyInfo.PropertyType.GetGenericArguments()[0];
                        EdiSegmentAttribute ediSegmentAttribute = (EdiSegmentAttribute)GenericArgument.GetCustomAttribute(typeof(EdiSegmentAttribute));
                        if (ediSegmentAttribute == null)
                        {
                            PropertyInfo[]      propertyInfosGeneric            = PropertyClass.GetPropertyInfos(GenericArgument.GetProperties(), typeof(EdiSegmentAttribute));
                            EdiSegmentAttribute ediSegmentAttributePropertyType = (EdiSegmentAttribute)propertyInfosGeneric[0].PropertyType.GetCustomAttribute(typeof(EdiSegmentAttribute));
                            if (ediSegmentAttributePropertyType.IsLoop)
                            {
                                while (ReadToEndLines.Count() > 1)
                                {
                                    object obj = DeserializeInternal(ref ReadToEndLines, GenericArgument);
                                    list.Add(obj);
                                }
                            }
                            else
                            {
                                object obj = DeserializeInternal(ref ReadToEndLines, GenericArgument);
                                list.Add(obj);
                            }
                        }
                        else
                        {
                            while (ediSegmentAttribute.Path == ReadToEndLines[0].Substring(0, 3))
                            {
                                object obj = DeserializeInternal(ref ReadToEndLines, GenericArgument);
                                list.Add(obj);
                            }
                        }
                        propertyInfo.SetValue(dataObject, list, null);
                    }
                    else
                    {
                        object         dataObject2        = Activator.CreateInstance(propertyInfo.PropertyType);
                        PropertyInfo[] PropertyInfoIsType = PropertyClass.GetPropertyInfos(dataObject2.GetType().GetProperties(), typeof(EdiOrderAttribute));
                        ReadToEndLines = IsType(ReadToEndLines, dataObject2, PropertyInfoIsType);
                        propertyInfo.SetValue(dataObject, dataObject2, null);
                    }
                }
            }
            return(dataObject);
        }
Exemplo n.º 8
0
        private string[] IsType(string[] ReadToEndLines, object dataObject, PropertyInfo[] PropertyInfos)
        {
            if (ReadToEndLines.Length == 0)
            {
                return(ReadToEndLines);
            }
            EdiSegmentAttribute ediSegmentAttribute = (EdiSegmentAttribute)dataObject.GetType().GetCustomAttribute(typeof(EdiSegmentAttribute));

            if ((ediSegmentAttribute != null) && (ediSegmentAttribute.Path != ReadToEndLines[0].Substring(0, 3)))
            {
                return(ReadToEndLines);
            }
            string[] Lines = null;
            if (ReadToEndLines[0].Contains("+"))
            {
                Lines = ReadToEndLines[0].Split('+');
            }
            else
            {
                Lines = ReadToEndLines;
            }

            if ((ediSegmentAttribute != null) && (Lines[0] == ediSegmentAttribute.Path))
            {
                ReadToEndLines = ReadToEndLines.Skip(1).ToArray();
                Lines          = Lines.Skip(1).ToArray();
            }
            for (int i = 0; i < Lines.Length; i++)
            {
                PropertyInfo      propertyInfo      = PropertyInfos[i];
                EdiOrderAttribute EdiOrderAttribute = (EdiOrderAttribute)propertyInfo.GetCustomAttribute(typeof(EdiOrderAttribute));
                if ((EdiOrderAttribute != null) && (EdiOrderAttribute.IsDetail))
                {
                    object         dataObject3      = Activator.CreateInstance(propertyInfo.PropertyType);
                    PropertyInfo[] PropertyInfosTwo = PropertyClass.GetPropertyInfos(dataObject3.GetType().GetProperties(), typeof(EdiOrderAttribute));
                    IsType(Lines[i].Split(':'), dataObject3, PropertyInfosTwo);
                    propertyInfo.SetValue(dataObject, dataObject3, null);
                }
                else
                {
                    if (propertyInfo.PropertyType == typeof(DateTime))
                    {
                        DateTime dateTime = DateTime.ParseExact(Lines[i], EdiOrderAttribute.Format, CultureInfo.InvariantCulture);
                        propertyInfo.SetValue(dataObject, dateTime);
                    }
                    else if (propertyInfo.PropertyType == typeof(int))
                    {
                        propertyInfo.SetValue(dataObject, Convert.ToInt32(Lines[i]));
                    }
                    else if (propertyInfo.PropertyType == typeof(TimeSpan))
                    {
                        TimeSpan timeSpan = TimeSpan.ParseExact(Lines[i], EdiOrderAttribute.Format, CultureInfo.InvariantCulture);
                        propertyInfo.SetValue(dataObject, timeSpan);
                    }
                    else
                    {
                        propertyInfo.SetValue(dataObject, Lines[i]);
                    }
                }
            }
            return(ReadToEndLines);
        }
Exemplo n.º 9
0
        private static void SetEdiSegmentAttribute(StreamWriter streamWriter, object objectTypeDetail, EdiSegmentAttribute ediSegmentAttribute)
        {
            string lineInfo = string.Empty;

            lineInfo += ediSegmentAttribute.Path + Separate;
            foreach (PropertyInfo propertyInfoDetail in PropertyInfoClass.GetPropertyInfos(objectTypeDetail))
            {
                object[] EdiValueAttributes = propertyInfoDetail.GetCustomAttributes(typeof(EdiValueAttribute), true);
                foreach (object ediValueAttribute in EdiValueAttributes)
                {
                    EdiValueAttribute ediValueAttributeInfo = ediValueAttribute as EdiValueAttribute;
                    string            detailValue           = string.Empty;
                    switch (ediValueAttributeInfo.Picture.Kind)
                    {
                    case Enums.PictureKind.Alphanumeric:
                        if (propertyInfoDetail.PropertyType == typeof(string))
                        {
                            detailValue = (string)propertyInfoDetail.GetValue(objectTypeDetail);
                            if (ediValueAttributeInfo.IsTrim)
                            {
                                if (detailValue != null && detailValue.Length > ediValueAttributeInfo.Picture.Scale)
                                {
                                    detailValue = detailValue.Substring(0, ediValueAttributeInfo.Picture.Scale);
                                }
                            }
                            else
                            {
                                detailValue = detailValue.PadRight(ediValueAttributeInfo.Picture.Scale, ' ');
                            }
                        }
                        else if (propertyInfoDetail.PropertyType == typeof(char))
                        {
                            detailValue = ((char)propertyInfoDetail.GetValue(objectTypeDetail)).ToString();
                            if (ediValueAttributeInfo.IsTrim)
                            {
                                if (detailValue != null && detailValue.Length > ediValueAttributeInfo.Picture.Scale)
                                {
                                    detailValue = detailValue.Substring(0, ediValueAttributeInfo.Picture.Scale);
                                }
                            }
                            else
                            {
                                detailValue = detailValue.PadRight(ediValueAttributeInfo.Picture.Scale, ' ');
                            }
                        }
                        else if (propertyInfoDetail.PropertyType.IsGenericType && propertyInfoDetail.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                        {
                            var genericType = propertyInfoDetail.PropertyType.GetGenericArguments()[0];
                            if (genericType.FullName == "System.Char")
                            {
                                detailValue = ((char)propertyInfoDetail.GetValue(objectTypeDetail)).ToString();
                                if (ediValueAttributeInfo.IsTrim)
                                {
                                    detailValue = detailValue.PadRight(ediValueAttributeInfo.Picture.Scale, ' ');
                                }
                                else
                                {
                                    if (detailValue != null && detailValue.Length > ediValueAttributeInfo.Picture.Scale)
                                    {
                                        detailValue = detailValue.Substring(0, ediValueAttributeInfo.Picture.Scale);
                                    }
                                }
                            }
                        }
                        else
                        {
                            throw new Exception("Hata");
                        }
                        break;

                    case Enums.PictureKind.Numeric:
                        if (propertyInfoDetail.GetValue(objectTypeDetail) == null)
                        {
                            var a = propertyInfoDetail.GetType();
                            detailValue = string.Empty;
                            break;
                        }
                        else
                        {
                            detailValue = propertyInfoDetail.GetValue(objectTypeDetail).ToString();
                        }
                        if (detailValue == "True")
                        {
                            detailValue = "1";
                        }
                        if (detailValue == "False")
                        {
                            detailValue = "0";
                        }
                        if (ediValueAttributeInfo.Format != null)
                        {
                            detailValue = DateTime.Parse(detailValue).ToString(ediValueAttributeInfo.Format);
                            detailValue = detailValue.PadLeft(ediValueAttributeInfo.Picture.Scale, '0');
                        }
                        else
                        {
                            detailValue = detailValue.PadLeft(ediValueAttributeInfo.Picture.Scale, '0');
                        }
                        break;

                    default:
                        break;
                    }
                    if (detailValue != null)
                    {
                        lineInfo += detailValue + Separate;
                    }
                }
            }
            lineInfo = lineInfo.Substring(0, lineInfo.Length - 1) + "~";
            streamWriter.WriteLine(lineInfo);
        }
Exemplo n.º 10
0
        private string WriteObjectType(StreamWriter streamWriter, object objectType, string separate)
        {
            string result = string.Empty;

            PropertyInfo[] propertyInfos = PropertyClass.GetPropertyInfos(objectType.GetType().GetProperties(), typeof(EdiSegmentAttribute));
            if (propertyInfos.Length > 0)
            {
                result = string.Empty;
                foreach (PropertyInfo propertyInfo in propertyInfos.OrderBy(p => p.GetCustomAttributes().OfType <EdiSegmentAttribute>().First().Order).ToArray())
                {
                    if (!propertyInfo.PropertyType.IsGenericType)
                    {
                        object values = propertyInfo.GetValue(objectType);
                        if (values != null)
                        {
                            EdiSegmentAttribute propertyTypeAttribute = (EdiSegmentAttribute)propertyInfo.PropertyType.GetCustomAttribute(typeof(EdiSegmentAttribute));
                            if (propertyTypeAttribute != null)
                            {
                                result += propertyTypeAttribute.Path + "+" + WriteObjectType(streamWriter, values, separate);
                                result  = result.Substring(0, result.Length - 1) + "'";
                                streamWriter.WriteLine(result);
                                result = string.Empty;
                            }
                            else
                            {
                                throw new Exception($"The model {propertyInfo.Name} is incorrect.");
                            }
                        }
                    }
                    else
                    {
                        IList values = (IList)propertyInfo.GetValue(objectType);
                        if (values == null)
                        {
                            continue;
                        }
                        for (int i = 0; i < values.Count; i++)
                        {
                            EdiSegmentAttribute propertyTypeAttribute = (EdiSegmentAttribute)values[i].GetType().GetCustomAttribute(typeof(EdiSegmentAttribute));
                            if (propertyTypeAttribute == null)
                            {
                                result = WriteObjectType(streamWriter, values[i], separate);
                            }
                            else
                            {
                                result += propertyTypeAttribute.Path + "+" + WriteObjectType(streamWriter, values[i], separate);
                                result  = result.Substring(0, result.Length - 1) + "'";
                                streamWriter.WriteLine(result);
                                result = string.Empty;
                            }
                        }
                    }
                }
            }

            propertyInfos = PropertyClass.GetPropertyInfos(objectType.GetType().GetProperties(), typeof(EdiOrderAttribute));
            if (propertyInfos.Length > 0)
            {
                foreach (PropertyInfo propertyInfo in propertyInfos.OrderBy(p => p.GetCustomAttributes().OfType <EdiOrderAttribute>().First().Order).ToArray())
                {
                    EdiOrderAttribute ediOrderAttribute = (EdiOrderAttribute)propertyInfo.GetCustomAttribute(typeof(EdiOrderAttribute));
                    if (ediOrderAttribute.IsDetail)
                    {
                        object values = propertyInfo.GetValue(objectType);
                        if (values != null)
                        {
                            result += WriteObjectType(streamWriter, values, ":") + "+";
                        }
                    }
                    else
                    {
                        if (propertyInfo.PropertyType == typeof(string))
                        {
                            if (propertyInfo.GetValue(objectType) != null)
                            {
                                result += (string)propertyInfo.GetValue(objectType) + separate;
                            }
                        }
                        else if (propertyInfo.PropertyType == typeof(int))
                        {
                            result += ((int)propertyInfo.GetValue(objectType)).ToString() + separate;
                        }
                        else if (propertyInfo.PropertyType == typeof(DateTime))
                        {
                            if (propertyInfo.GetValue(objectType) != null)
                            {
                                string   daetTimeStr    = propertyInfo.GetValue(objectType).ToString();
                                DateTime dateTime       = DateTime.Parse(daetTimeStr);
                                string   dateTimeResult = dateTime.ToString(ediOrderAttribute.Format, null);
                                result += dateTimeResult + separate;
                            }
                        }
                        else if (propertyInfo.PropertyType == typeof(TimeSpan))
                        {
                            if (propertyInfo.GetValue(objectType) != null)
                            {
                                string   timeSpanStr    = propertyInfo.GetValue(objectType).ToString();
                                TimeSpan timeSpan       = TimeSpan.Parse(timeSpanStr);
                                string   timeSpanResult = timeSpan.ToString(ediOrderAttribute.Format, null);
                                result += timeSpanResult + separate;
                            }
                        }
                    }
                }
            }

            if (result != string.Empty)
            {
                if (result.Substring(result.Length - 1, 1) == ":")
                {
                    return(result.Substring(0, result.Length - 1));
                }
                else
                {
                    return(result);
                }
            }
            else
            {
                return(result);
            }
        }