public static object Deserialize(Type type, XElement element, SerializerOptions options) { var attProps = type.GetProperties().Where(x => x.HasAttribute <DFeAttributeAttribute>()).ToArray(); var valueProp = type.GetProperties().SingleOrDefault(x => x.HasAttribute <DFeItemValueAttribute>()); var item = type.HasCreate() ? type.GetCreate().Invoke() : Activator.CreateInstance(type); if (element == null) { return(item); } var valueAtt = valueProp.GetAttribute <DFeItemValueAttribute>(); var value = PrimitiveSerializer.GetValue(valueAtt.Tipo, element.Value, item, valueProp); valueProp.SetValue(item, value); foreach (var property in attProps) { var attTag = property.GetAttribute <DFeAttributeAttribute>(); var attElement = element.Attribute(attTag.Name); value = PrimitiveSerializer.Deserialize(attTag, attElement, item, property); property.SetValue(item, value); } return(item); }
/// <summary> /// Deserializes the specified type. /// </summary> /// <param name="type">The type of the list to deserialize.</param> /// <param name="parent">The parent.</param> /// <param name="prop">The property.</param> /// <param name="parentItem"></param> /// <param name="options">Indicates how the output is deserialized.</param> /// <returns>The deserialized list from the XElement.</returns> /// <exception cref="System.InvalidOperationException">Could not deserialize this non generic dictionary without more type information.</exception> /// Deserializes the XElement to the list (e.g. List<T />, Array of a specified type using options. public static object Deserialize(Type type, XElement[] parent, PropertyInfo prop, object parentItem, SerializerOptions options) { var listItemType = GetListType(type); var objectType = ObjectType.From(GetItemType(prop.PropertyType)); var list = (IList)Activator.CreateInstance(type); var elementAtt = prop.GetAttribute <DFeCollectionAttribute>(); if (prop.HasAttribute <DFeItemAttribute>()) { var itemTags = prop.GetAttributes <DFeItemAttribute>(); var elements = parent.All(x => x.Name.LocalName == elementAtt.Name) && parent.Length > 1 ? parent : parent.Elements(); foreach (var element in elements) { var itemTag = itemTags.SingleOrDefault(x => x.Name == element.Name.LocalName); Guard.Against <ACBrDFeException>(itemTag == null, $"Nenhum atributo [{nameof(DFeItemAttribute)}] encontrado " + $"para o elemento: {element.Name.LocalName}"); object obj; if (itemTag.IsValue) { obj = itemTag.Tipo.HasCreate() ? itemTag.Tipo.GetCreate().Invoke() : Activator.CreateInstance(itemTag.Tipo); var properties = itemTag.Tipo.GetProperties() .Where(x => !x.ShouldIgnoreProperty() && x.ShouldSerializeProperty(obj)) .OrderBy(x => x.GetAttribute <DFeBaseAttribute>()?.Ordem ?? 0).ToArray(); Guard.Against <ACBrDFeException>(!properties.All(x => x.HasAttribute <DFeItemValueAttribute>() || x.HasAttribute <DFeAttributeAttribute>()), $"Item {itemTag.Tipo.Name} é do tipo [ItemValue] e so pode ter atributo do tipo [DFeAttributeAttribute] ou [DFeItemValueAttribute]."); Guard.Against <ACBrDFeException>(properties.Count(x => x.HasAttribute <DFeItemValueAttribute>()) != 1, $"Item {itemTag.Tipo.Name} é do tipo [ItemValue] e não tem presente o atributo [DFeItemValueAttribute] ou possui mais de um atributo."); var valueProp = properties.SingleOrDefault(x => x.HasAttribute <DFeItemValueAttribute>()); var valueAtt = valueProp.GetAttribute <DFeItemValueAttribute>(); var value = PrimitiveSerializer.GetValue(valueAtt.Tipo, element.Value, obj, prop); valueProp.SetValue(obj, value); foreach (var property in properties.Where(x => x.HasAttribute <DFeAttributeAttribute>())) { var attTag = property.GetAttribute <DFeAttributeAttribute>(); value = PrimitiveSerializer.Deserialize(attTag, element, obj, property); property.SetValue(obj, value); } } else { obj = ObjectSerializer.Deserialize(itemTag.Tipo, element, options); } list.Add(obj); } } else { if (objectType == ObjectType.PrimitiveType) { foreach (var element in parent) { var obj = PrimitiveSerializer.Deserialize(elementAtt, element, parentItem, prop); list.Add(obj); } } else { if (ObjectType.From(prop.PropertyType).IsIn(ObjectType.ArrayType, ObjectType.EnumerableType)) { listItemType = GetItemType(prop.PropertyType); } foreach (var element in parent) { var obj = ObjectSerializer.Deserialize(listItemType, element, options); list.Add(obj); } } } return(list); }
/// <summary> /// Deserializes the specified type. /// </summary> /// <param name="type">The type of the list to deserialize.</param> /// <param name="parent">The parent.</param> /// <param name="prop">The property.</param> /// <param name="parentItem"></param> /// <param name="options">Indicates how the output is deserialized.</param> /// <returns>The deserialized list from the XElement.</returns> /// <exception cref="System.InvalidOperationException">Could not deserialize this non generic dictionary without more type information.</exception> /// Deserializes the XElement to the list (e.g. List<T />, Array of a specified type using options. public static object Deserialize(Type type, XElement[] parent, PropertyInfo prop, object parentItem, SerializerOptions options) { var listItemType = GetListType(type); var objectType = ObjectType.From(GetItemType(prop.PropertyType)); var list = (IList)Activator.CreateInstance(type); var elementAtt = prop.GetAttribute <DFeCollectionAttribute>(); if (prop.HasAttribute <DFeItemAttribute>()) { var itemTags = prop.GetAttributes <DFeItemAttribute>(); var elements = parent.All(x => x.Name.LocalName == elementAtt.Name) && parent.Length > 1 ? parent : parent.Elements(); foreach (var element in elements) { var itemTag = itemTags.SingleOrDefault(x => x.Name == element.Name.LocalName); Guard.Against <ACBrDFeException>(itemTag == null, $"Nenhum atributo [{nameof(DFeItemAttribute)}] encontrado " + $"para o elemento: {element.Name.LocalName}"); object item; if (itemTag.IsValue) { item = itemTag.Tipo.HasCreate() ? itemTag.Tipo.GetCreate().Invoke() : Activator.CreateInstance(itemTag.Tipo); var properties = itemTag.Tipo.GetPropsAndValidate(item); var valueProp = properties.SingleOrDefault(x => x.HasAttribute <DFeItemValueAttribute>()); var valueAtt = valueProp.GetAttribute <DFeItemValueAttribute>(); var value = PrimitiveSerializer.GetValue(valueAtt.Tipo, element.Value, item, prop); valueProp.SetValue(item, value); foreach (var property in properties.Where(x => x.HasAttribute <DFeAttributeAttribute>())) { var attTag = property.GetAttribute <DFeAttributeAttribute>(); value = PrimitiveSerializer.Deserialize(attTag, element, item, property); property.SetValue(item, value); } } else { item = ObjectSerializer.Deserialize(itemTag.Tipo, element, options); } list.Add(item); } } else { if (objectType == ObjectType.PrimitiveType) { foreach (var element in parent) { var obj = PrimitiveSerializer.Deserialize(elementAtt, element, parentItem, prop); list.Add(obj); } } else { if (ObjectType.From(prop.PropertyType).IsIn(ObjectType.ArrayType, ObjectType.EnumerableType)) { listItemType = GetItemType(prop.PropertyType); } foreach (var element in parent) { var obj = ObjectSerializer.Deserialize(listItemType, element, options); list.Add(obj); } } } return(list); }