Пример #1
0
        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)ListSerializer.Deserialize(typeof(ArrayList), listElement, options);
                    var type        = ListSerializer.GetItemType(att.Tipo);
                    return(objectType == ObjectType.ArrayType ? list.ToArray(type) : list.Cast(type));
                }

                if (objectType == ObjectType.ListType)
                {
                    var listElement = parentElement.ElementsAnyNs(att.Name);
                    return(ListSerializer.Deserialize(att.Tipo, listElement, options));
                }

                return(ObjectSerializer.Deserialize(att.Tipo, node, options));
            }

            return(null);
        }
Пример #2
0
        /// <summary>
        /// Deserializes the XElement to the object of a specified type using options.
        /// </summary>
        /// <param name="prop">The property.</param>
        /// <param name="parentElement">The parent XElement used to deserialize the object.</param>
        /// <param name="item">The item.</param>
        /// <param name="options">Indicates how the output is deserialized.</param>
        /// <returns>The deserialized object from the XElement.</returns>
        /// <exception cref="System.NotSupportedException">Tipo Dictionary não suportado ainda !</exception>
        public static object Deserialize(PropertyInfo prop, XElement parentElement, object item, SerializerOptions options)
        {
            try
            {
                var tag = prop.HasAttribute <DFeElementAttribute>() ? (IDFeElement)prop.GetAttribute <DFeElementAttribute>() : prop.GetAttribute <DFeAttributeAttribute>();

                var objectType = ObjectType.From(prop.PropertyType);
                if (objectType == ObjectType.DictionaryType)
                {
                    throw new NotSupportedException("Tipo Dictionary não suportado ainda !");
                }

                if (objectType.IsIn(ObjectType.ArrayType, ObjectType.EnumerableType))
                {
                    var listElement = parentElement.ElementsAnyNs(tag.Name);
                    var list        = (ArrayList)ListSerializer.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(ListSerializer.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));
                }

                var element = parentElement.ElementsAnyNs(tag.Name).FirstOrDefault() ??
                              (XObject)parentElement.Attributes(tag.Name).FirstOrDefault();

                return(PrimitiveSerializer.Deserialize(tag, element, item, prop, options));
            }
            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);
            }
        }