Пример #1
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);
        }
Пример #2
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);
        }
Пример #3
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);
            }
        }