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); }
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); } }