public static object Deserialize(PropertyInfo prop, XElement parentElement, object item, SerializerOptions options) { var tags = prop.GetAttributes <DFeItemAttribute>(); foreach (var att in tags) { var node = parentElement.ElementsAnyNs(att.Name).FirstOrDefault(); if (node == null) { continue; } var objectType = ObjectType.From(att.Tipo); if (objectType.IsIn(ObjectType.ArrayType, ObjectType.EnumerableType)) { var listElement = parentElement.ElementsAnyNs(att.Name); var list = (ArrayList)CollectionSerializer.Deserialize(typeof(ArrayList), listElement, options); var type = CollectionSerializer.GetItemType(att.Tipo); return(objectType == ObjectType.ArrayType ? list.ToArray(type) : list.Cast(type)); } if (objectType == ObjectType.ListType) { var listElement = parentElement.ElementsAnyNs(att.Name); return(CollectionSerializer.Deserialize(att.Tipo, listElement, options)); } return(objectType == ObjectType.ValueElementType ? ValueElementSerializer.Deserialize(att.Tipo, parentElement, options) : ObjectSerializer.Deserialize(att.Tipo, node, options)); } return(null); }
public static object Deserialize(PropertyInfo prop, XElement parentElement, object item, SerializerOptions options) { try { var tag = prop.HasAttribute <DFeElementAttribute>() ? (DFeBaseAttribute)prop.GetAttribute <DFeElementAttribute>() : prop.GetAttribute <DFeAttributeAttribute>(); var objectType = ObjectType.From(prop.PropertyType); if (objectType == ObjectType.DictionaryType) { var dicTag = prop.GetAttribute <DFeDictionaryAttribute>(); var dictionaryElement = parentElement.ElementAnyNs(dicTag.Name); return(DictionarySerializer.Deserialize(prop, dictionaryElement, item, options)); } if (objectType.IsIn(ObjectType.ArrayType, ObjectType.EnumerableType)) { var listElement = parentElement.ElementsAnyNs(tag.Name); var list = (ArrayList)CollectionSerializer.Deserialize(typeof(ArrayList), listElement.ToArray(), prop, item, options); var type = prop.PropertyType.IsArray ? prop.PropertyType.GetElementType() : prop.PropertyType.GetGenericArguments()[0]; return(objectType == ObjectType.ArrayType ? list.ToArray(type) : list.Cast(type)); } if (objectType == ObjectType.ListType) { var listElement = parentElement.ElementsAnyNs(tag.Name); return(CollectionSerializer.Deserialize(prop.PropertyType, listElement.ToArray(), prop, item, options)); } if (objectType.IsIn(ObjectType.InterfaceType, ObjectType.AbstractType)) { return(InterfaceSerializer.Deserialize(prop, parentElement, item, options)); } if (objectType == ObjectType.RootType) { if (tag != null) { var xElement = parentElement.ElementsAnyNs(tag.Name).FirstOrDefault(); return(Deserialize(prop.PropertyType, xElement, options)); } var rootTag = prop.PropertyType.GetAttribute <DFeRootAttribute>(); var rootNames = new List <string>(); if (!rootTag.Name.IsEmpty()) { rootNames.Add(rootTag.Name); rootNames.Add(prop.PropertyType.Name); } else { rootNames.AddRange(prop.PropertyType.GetRootNames()); rootNames.Add(prop.PropertyType.Name); } var xmlNode = (from node in parentElement.Elements() where node.Name.LocalName.IsIn(rootNames) select node).FirstOrDefault(); return(Deserialize(prop.PropertyType, xmlNode, options)); } if (objectType == ObjectType.ClassType) { var xElement = parentElement.ElementsAnyNs(tag.Name).FirstOrDefault(); return(Deserialize(prop.PropertyType, xElement, options)); } XObject element; if (tag is DFeAttributeAttribute) { element = parentElement.Attributes(tag.Name).FirstOrDefault(); } else { element = parentElement.ElementsAnyNs(tag.Name).FirstOrDefault(); } return(PrimitiveSerializer.Deserialize(tag, element, item, prop)); } catch (Exception e) { var msg = $"Erro ao deserializar a propriedade:{Environment.NewLine}{prop.DeclaringType?.Name ?? prop.PropertyType.Name} - {prop.Name}"; logger.Error(msg, e); throw new ACBrDFeException(msg, e); } }