public static XObject[] SerializeObjects(ICollection values, DFeItemAttribute tag, SerializerOptions options)
 {
     return((from object value in values select ObjectSerializer.Serialize(value, value.GetType(), tag.Name, tag.Namespace, options)).Cast <XObject>().ToArray());
 }
        /// <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 });
        }