Пример #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);
        }
        /// <summary>
        /// Serializes the specified value.
        /// </summary>
        /// <param name="prop">The property.</param>
        /// <param name="parentObject">The parent object.</param>
        /// <param name="options">The options.</param>
        /// <returns>XElement.</returns>
        public static XObject[] Serialize(PropertyInfo prop, object parentObject, SerializerOptions options)
        {
            var tag  = prop.GetAttribute <DFeCollectionAttribute>();
            var list = (ICollection)prop.GetValue(parentObject, null) ?? new ArrayList();

            if (list.Count < tag.MinSize || list.Count > tag.MaxSize && tag.MaxSize > 0)
            {
                var msg = list.Count > tag.MaxSize ? DFeSerializer.ErrMsgMaiorMaximo : DFeSerializer.ErrMsgMenorMinimo;
                options.AddAlerta(tag.Id, tag.Name, tag.Descricao, msg);
            }

            if (list.Count == 0 && tag.MinSize == 0 && tag.Ocorrencia == Ocorrencia.NaoObrigatoria)
            {
                return(null);
            }

            var itemType   = GetItemType(prop.PropertyType) ?? GetItemType(list.GetType());
            var objectType = ObjectType.From(itemType);

            if (objectType == ObjectType.PrimitiveType)
            {
                return(SerializePrimitive(prop, parentObject, list, tag, options));
            }

            return(!prop.HasAttribute <DFeItemAttribute>() ? SerializeObjects(list, tag, options) :
                   SerializeChild(list, tag, prop.GetAttributes <DFeItemAttribute>(), options));
        }
Пример #3
0
        public static XElement[] SerializeElementValue(ICollection values, DFeCollectionAttribute tag, DFeItemAttribute[] itemTags, SerializerOptions options)
        {
            var retElements = new List <XElement>();

            foreach (var value in values)
            {
                var itemTag = itemTags.SingleOrDefault(x => x.Tipo == value.GetType());
                Guard.Against <ACBrDFeException>(itemTag == null, $"Item {value.GetType().Name} não presente na lista de itens.");

                var properties = value.GetType().GetProperties()
                                 .Where(x => !x.ShouldIgnoreProperty() && x.ShouldSerializeProperty(value))
                                 .OrderBy(x => x.GetAttribute <DFeBaseAttribute>()?.Ordem ?? 0).ToArray();

                var valueProp = properties.Single(x => x.HasAttribute <DFeItemValueAttribute>());

                var valueType = ObjectType.From(valueProp.PropertyType);
                Guard.Against <ACBrDFeException>(valueType != ObjectType.PrimitiveType,
                                                 $"Item {value.GetType().Name} é do tipo [ItemValue] e o [DFeItemValueAttribute] não é do tipo primitivo");

                var attProps = properties.Where(x => x.HasAttribute <DFeAttributeAttribute>()).ToArray();

                var element = ValueElementSerializer.Serialize(itemTag.Name, itemTag.Namespace, value, options, valueProp, attProps);
                retElements.Add(element);
            }

            return(retElements.ToArray());
        }
Пример #4
0
        public static XObject[] Serialize(PropertyInfo prop, object parentObject, SerializerOptions options)
        {
            var value         = prop.GetValue(parentObject, null);
            var objectType    = ObjectType.From(value);
            var attributes    = prop.GetAttributes <DFeItemAttribute>();
            var itemAttribute = attributes.SingleOrDefault(x => x.Tipo == value.GetType()) ?? attributes[0];

            if (objectType.IsIn(ObjectType.ListType, ObjectType.ArrayType, ObjectType.EnumerableType))
            {
                var list = (ICollection)prop.GetValue(parentObject, null);
                return(ListSerializer.SerializeObjects(list, itemAttribute, options));
            }

            return(new XObject[] { ObjectSerializer.Serialize(value, value.GetType(), itemAttribute.Name, options) });
        }
        public static object Deserialize(PropertyInfo prop, XElement parent, object parentItem, SerializerOptions options)
        {
            Guard.Against <ACBrDFeException>(!prop.HasAttribute <DFeDictionaryAttribute>(), $"Atributo necessário não encontrado [{nameof(DFeDictionaryAttribute)}]");
            Guard.Against <ACBrDFeException>(!prop.HasAttribute <DFeDictionaryKeyAttribute>(), $"Atributo necessário não encontrado [{nameof(DFeDictionaryKeyAttribute)}]");
            Guard.Against <ACBrDFeException>(!prop.HasAttribute <DFeDictionaryValueAttribute>(), $"Atributo necessário não encontrado [{nameof(DFeDictionaryValueAttribute)}]");

            var tag      = prop.GetAttribute <DFeDictionaryAttribute>();
            var keyAtt   = prop.GetAttribute <DFeDictionaryKeyAttribute>();
            var valueAtt = prop.GetAttribute <DFeDictionaryValueAttribute>();

            var dictionary = (IDictionary)Activator.CreateInstance(prop.PropertyType);
            var args       = prop.PropertyType.GetGenericArguments();

            var keyType   = ObjectType.From(args[0]);
            var valueType = ObjectType.From(args[1]);

            var elements = parent.ElementsAnyNs(keyAtt.AsAttribute ? valueAtt.Name : tag.ItemName);

            foreach (var element in elements)
            {
                object key;
                object value;
                if (keyAtt.AsAttribute)
                {
                    var keyElement = (XObject)element.Attributes(keyAtt.Name).FirstOrDefault();
                    key   = PrimitiveSerializer.Deserialize(keyAtt, keyElement, null, prop);
                    value = valueType == ObjectType.PrimitiveType ?
                            PrimitiveSerializer.Deserialize(valueAtt, element, parentItem, prop) :
                            ObjectSerializer.Deserialize(args[1], element, options);
                }
                else
                {
                    key = keyType == ObjectType.PrimitiveType ?
                          PrimitiveSerializer.Deserialize(keyAtt, element.ElementAnyNs(keyAtt.Name), parentItem, prop) :
                          ObjectSerializer.Deserialize(args[0], element.ElementAnyNs(keyAtt.Name), options);

                    value = valueType == ObjectType.PrimitiveType ?
                            PrimitiveSerializer.Deserialize(valueAtt, element.ElementAnyNs(valueAtt.Name), parentItem, prop) :
                            ObjectSerializer.Deserialize(args[1], element.ElementAnyNs(valueAtt.Name), options);
                }

                dictionary.Add(key, value);
            }

            return(dictionary);
        }
Пример #6
0
        /// <summary>
        /// Serializes the specified value.
        /// </summary>
        /// <param name="prop">The property.</param>
        /// <param name="parentObject">The parent object.</param>
        /// <param name="options">The options.</param>
        /// <returns>XElement.</returns>
        public static XObject[] Serialize(PropertyInfo prop, object parentObject, SerializerOptions options)
        {
            var tag  = prop.GetAttribute <DFeCollectionAttribute>();
            var list = (ICollection)prop.GetValue(parentObject, null) ?? new ArrayList();

            if (list.Count < tag.MinSize || list.Count > tag.MaxSize && tag.MaxSize > 0)
            {
                var msg = list.Count > tag.MaxSize ? DFeSerializer.ErrMsgMaiorMaximo : DFeSerializer.ErrMsgMenorMinimo;
                options.AddAlerta(tag.Id, tag.Name, tag.Descricao, msg);
            }

            if (list.Count == 0 && tag.MinSize == 0 && tag.Ocorrencia == Ocorrencia.NaoObrigatoria)
            {
                return(null);
            }

            XElement arrayElement = null;

            if (!tag.Name.IsEmpty() && prop.HasAttribute <DFeItemAttribute>())
            {
                arrayElement = new XElement(tag.Name);
            }

            var itemType   = GetItemType(prop.PropertyType) ?? GetItemType(list.GetType());
            var objectType = ObjectType.From(itemType);

            XElement[] childs;
            if (objectType == ObjectType.PrimitiveType)
            {
                childs = SerializePrimitive(prop, parentObject, list, tag, options);
            }
            else if (objectType == ObjectType.ValueElementType)
            {
                childs = SerializeElementValue(list, tag, prop.GetAttributes <DFeItemAttribute>(), options);
            }
            else
            {
                childs = !prop.HasAttribute <DFeItemAttribute>() ? SerializeObjects(list, tag, options) :
                         SerializeChild(list, tag, prop.GetAttributes <DFeItemAttribute>(), options);
            }

            arrayElement?.AddChild(childs.ToArray());

            return(arrayElement != null ? new XObject[] { arrayElement } : childs.Cast <XObject>().ToArray());
        }
        public static XObject[] Serialize(PropertyInfo prop, object parentObject, SerializerOptions options)
        {
            var value         = prop.GetValue(parentObject, null);
            var objectType    = ObjectType.From(value);
            var attributes    = prop.GetAttributes <DFeItemAttribute>();
            var itemAttribute = attributes.SingleOrDefault(x => x.Tipo == value.GetType());

            Guard.Against <ACBrDFeException>(itemAttribute == null, $"Nenhum atributo [{nameof(DFeItemAttribute)}] encontrado " +
                                             $"para o objeto: {nameof(value.GetType)}");

            if (objectType.IsIn(ObjectType.ListType, ObjectType.ArrayType, ObjectType.EnumerableType))
            {
                var list = (ICollection)prop.GetValue(parentObject, null);
                return(CollectionSerializer.SerializeObjects(list, itemAttribute, options));
            }

            return(new XObject[] { ObjectSerializer.Serialize(value, value.GetType(), itemAttribute.Name, itemAttribute.Namespace, options) });
        }
Пример #8
0
        /// <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>();

            IEnumerable <XElement> elements = parent;

            if (prop.HasAttribute <DFeItemAttribute>())
            {
                var itemTags = prop.GetAttributes <DFeItemAttribute>();
                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) ?? itemTags[0];
                    var obj     = ObjectSerializer.Deserialize(itemTag.Tipo, element, options);
                    list.Add(obj);
                }
            }
            else
            {
                if (objectType == ObjectType.PrimitiveType)
                {
                    foreach (var element in elements)
                    {
                        var obj = PrimitiveSerializer.Deserialize(elementAtt, element, parentItem, prop, options);
                        list.Add(obj);
                    }
                }
                else
                {
                    foreach (var element in elements)
                    {
                        var obj = ObjectSerializer.Deserialize(listItemType, element, options);
                        list.Add(obj);
                    }
                }
            }

            return(list);
        }
        public static XObject[] Serialize(PropertyInfo prop, object parentObject, SerializerOptions options)
        {
            var properties = prop.PropertyType.GetProperties()
                             .Where(x => !x.ShouldIgnoreProperty() && x.ShouldSerializeProperty(parentObject))
                             .OrderBy(x => x.GetAttribute <DFeBaseAttribute>()?.Ordem ?? 0).ToArray();

            var valueProp = properties.Single(x => x.HasAttribute <DFeItemValueAttribute>());

            var valueType = ObjectType.From(valueProp.PropertyType);

            Guard.Against <ACBrDFeException>(valueType != ObjectType.PrimitiveType,
                                             $"Item {prop.Name} é do tipo [ItemValue] e o [DFeItemValueAttribute] não é do tipo primitivo");

            var value     = prop.GetValue(parentObject, null);
            var attribute = prop.GetAttribute <DFeElementAttribute>();
            var attProps  = properties.Where(x => x.HasAttribute <DFeAttributeAttribute>()).ToArray();

            return(new[] { Serialize(attribute.Name, attribute.Namespace, value, options, valueProp, attProps) });
        }
        /// <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);
        }
Пример #11
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);
            }
        }
Пример #12
0
        /// <summary>
        /// Serializes the specified property into a XElement using options.
        /// </summary>
        /// <param name="prop">The property to serialize.</param>
        /// <param name="parentObject">The object that owns the property.</param>
        /// <param name="options">Indicates how the output is formatted or serialized.</param>
        /// <returns>The XElement representation of the property. May be null if it has no value, cannot be read or written or should be ignored.</returns>
        public static IEnumerable <XObject> Serialize(PropertyInfo prop, object parentObject, SerializerOptions options)
        {
            try
            {
                var objectType = ObjectType.From(prop.PropertyType);

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

                if (objectType.IsIn(ObjectType.ListType, ObjectType.ArrayType, ObjectType.EnumerableType))
                {
                    return(ListSerializer.Serialize(prop, parentObject, options));
                }

                var value = prop.GetValue(parentObject, null);

                if (objectType.IsIn(ObjectType.InterfaceType, ObjectType.AbstractType))
                {
                    return(value == null ? null : InterfaceSerializer.Serialize(prop, parentObject, options));
                }

                if (objectType == ObjectType.ClassType)
                {
                    var attribute = prop.GetAttribute <DFeElementAttribute>();
                    if (attribute.Ocorrencia == Ocorrencia.NaoObrigatoria && value == null)
                    {
                        return(null);
                    }
                    return(new XObject[] { Serialize(value, prop.PropertyType, attribute.Name, options) });
                }

                if (objectType == ObjectType.RootType)
                {
                    if (prop.HasAttribute <DFeElementAttribute>())
                    {
                        var attribute = prop.GetAttribute <DFeElementAttribute>();
                        if (attribute.Ocorrencia == Ocorrencia.NaoObrigatoria && value == null)
                        {
                            return(null);
                        }
                        return(new XObject[] { Serialize(value, prop.PropertyType, attribute.Name, options) });
                    }

                    if (value == null)
                    {
                        return(null);
                    }
                    var rooTag   = prop.PropertyType.GetAttribute <DFeRootAttribute>();
                    var rootName = rooTag.Name;

                    if (rootName.IsEmpty())
                    {
                        var root = prop.PropertyType.GetRootName(value);
                        rootName = root.IsEmpty() ? prop.PropertyType.Name : root;
                    }

                    var rootElement = Serialize(value, prop.PropertyType, rootName, rooTag.Namespace, options);
                    return(new XObject[] { rootElement });
                }

                var tag = prop.GetTag();
                return(new[] { PrimitiveSerializer.Serialize(tag, parentObject, prop, options) });
            }
            catch (Exception e)
            {
                var msg = $"Erro ao serializar a propriedade:{Environment.NewLine}{prop.DeclaringType?.Name ?? prop.PropertyType.Name} - {prop.Name}";
                Logger.Error(msg, e);
                throw new ACBrDFeException(msg, e);
            }
        }
        /// <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);
        }
        public static XObject[] Serialize(PropertyInfo prop, object parentObject, SerializerOptions options)
        {
            Guard.Against <ACBrDFeException>(!prop.HasAttribute <DFeDictionaryAttribute>(), $"Atributo necessário não encontrado [{nameof(DFeDictionaryAttribute)}]");
            Guard.Against <ACBrDFeException>(!prop.HasAttribute <DFeDictionaryKeyAttribute>(), $"Atributo necessário não encontrado [{nameof(DFeDictionaryKeyAttribute)}]");
            Guard.Against <ACBrDFeException>(!prop.HasAttribute <DFeDictionaryValueAttribute>(), $"Atributo necessário não encontrado [{nameof(DFeDictionaryValueAttribute)}]");

            var tag      = prop.GetAttribute <DFeDictionaryAttribute>();
            var keyAtt   = prop.GetAttribute <DFeDictionaryKeyAttribute>();
            var valueAtt = prop.GetAttribute <DFeDictionaryValueAttribute>();

            Guard.Against <ArgumentNullException>(!keyAtt.AsAttribute && tag.ItemName.IsEmpty(), "Se a Key não é um atributo é necessario informar o [ItemName]");

            var dictionary = (IDictionary)prop.GetValue(parentObject, null);

            if (dictionary.Count < tag.MinSize || dictionary.Count > tag.MaxSize && tag.MaxSize > 0)
            {
                var msg = dictionary.Count > tag.MaxSize ? DFeSerializer.ErrMsgMaiorMaximo : DFeSerializer.ErrMsgMenorMinimo;
                options.AddAlerta(tag.Id, tag.Name, tag.Descricao, msg);
            }

            if (dictionary.Count == 0 && tag.MinSize == 0 && tag.Ocorrencia == Ocorrencia.NaoObrigatoria)
            {
                return(null);
            }

            var args = dictionary.GetType().GetGenericArguments();

            Guard.Against <ArgumentException>(args.Length != 2);

            var keyType   = ObjectType.From(args[0]);
            var valueType = ObjectType.From(args[1]);

            Guard.Against <ACBrDFeException>(keyType != ObjectType.PrimitiveType && keyAtt.AsAttribute);

            var list = new List <XElement>();

            var dicENumerator = dictionary.GetEnumerator();

            while (dicENumerator.MoveNext())
            {
                var key   = dicENumerator.Entry.Key;
                var value = dicENumerator.Entry.Value;

                var keyElement = keyType == ObjectType.PrimitiveType
                    ? PrimitiveSerializer.Serialize(keyAtt, key, options)
                    : ObjectSerializer.Serialize(key, key.GetType(), keyAtt.Name, keyAtt.Namespace, options);

                var valueElement = valueType == ObjectType.PrimitiveType
                    ? (XElement)PrimitiveSerializer.Serialize(valueAtt, value, options)
                    : ObjectSerializer.Serialize(value, value.GetType(), valueAtt.Name, valueAtt.Namespace, options);

                if (keyAtt.AsAttribute)
                {
                    valueElement.AddAttribute((XAttribute)keyElement);
                    list.Add(valueElement);
                }
                else
                {
                    var itemElement = new XElement(tag.ItemName);
                    itemElement.AddChild((XElement)keyElement);
                    itemElement.AddChild(valueElement);

                    list.Add(itemElement);
                }
            }

            var element = new XElement(tag.Name, tag.Namespace);

            element.AddChild(list.ToArray());

            return(new XObject[] { element });
        }