コード例 #1
0
 public bool IsInlineElementArray(Type entityType, XmlSerializerContext context)
 {
     if (this.IsXmlElement(entityType, context))
     {
         //tiene que estar el attributo de tipo inline
         XmlInlineArrayElementAttribute att = (XmlInlineArrayElementAttribute)this.Metadata.Attributes.Find(p => (p is XmlInlineArrayElementAttribute) && (((XmlInlineArrayElementAttribute)p).Type == null || ((XmlInlineArrayElementAttribute)p).Type.Equals(entityType)));
         return(att != null);
     }
     else
     {
         return(false);
     }
 }
コード例 #2
0
        public string GetInlineItemSeparator(Type entityType, XmlSerializerContext context, bool isAttribute)
        {
            if (isAttribute)
            {
                //si el tipo es nulo, entonces se busca el default
                XmlInlineArrayAttributeAttribute att = (XmlInlineArrayAttributeAttribute)this.Metadata.Attributes.Find(p => (p is XmlInlineArrayAttributeAttribute) && ((XmlInlineArrayAttributeAttribute)p).Type != null && ((XmlInlineArrayAttributeAttribute)p).Type.Equals(entityType));

                //busco el default inline
                if (att == null)
                {
                    att = (XmlInlineArrayAttributeAttribute)this.Metadata.Attributes.Find(p => (p is XmlInlineArrayAttributeAttribute) && ((XmlInlineArrayAttributeAttribute)p).Type == null);
                }

                if (att == null || string.IsNullOrEmpty(att.ItemSeparator))
                {
                    return(context.Settings.DefaultInlineListItemSeparator);
                }
                else
                {
                    return(att.ItemSeparator.Trim());
                }
            }
            else
            {
                XmlInlineArrayElementAttribute att = (XmlInlineArrayElementAttribute)this.Metadata.Attributes.Find(p => (p is XmlInlineArrayElementAttribute) && ((XmlInlineArrayElementAttribute)p).Type != null && ((XmlInlineArrayElementAttribute)p).Type.Equals(entityType));

                if (att == null)
                {
                    att = (XmlInlineArrayElementAttribute)this.Metadata.Attributes.Find(p => (p is XmlInlineArrayElementAttribute) && ((XmlInlineArrayElementAttribute)p).Type == null);
                }

                if (att == null || string.IsNullOrEmpty(att.ItemSeparator))
                {
                    return(context.Settings.DefaultInlineListItemSeparator);
                }
                else
                {
                    return(att.ItemSeparator.Trim());
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Devuelve un booleano indicando que el atributo es de serializacion en xml
        /// </summary>
        /// <param name="att"></param>
        /// <returns></returns>
        protected virtual bool InitializeAttribute(Attribute att)
        {
            bool output = false;

            if (att is XmlElementValueAttribute)
            {
                XmlElementValueAttribute a = att as XmlElementValueAttribute;
                if (a.Type == null)
                {
                    if (TypeHelper.IsValueType(a.Type))
                    {
                        this.DefaultAttributeName = a.AttributeName;
                    }
                    this.DefaultElementName = a.ElementName;
                }
                else
                {
                    if (TypeHelper.IsValueType(a.Type))
                    {
                        this.AddAttributeToTypeMap(a.AttributeName, a.Type);
                        this.AddTypeToAttributeMap(a.Type, a.AttributeName);
                    }

                    this.AddElementToTypeMap(a.ElementName, a.Type);
                    this.AddTypeToElementMap(a.Type, a.ElementName);

                    if (!this.IncludeTypes.Contains(a.Type))
                    {
                        this.AddIncludeTypes(a.Type);
                    }
                }

                output = true;
            }
            else if (att is XmlContentAttribute)
            {
                this.AddAttribute(att);
                output = true;
            }
            else if (att is XmlDateTimeFormatAttribute)
            {
                this.AddAttribute(att);
                output = true;
            }
            else if (att is XmlTimeSpanFormatAttribute)
            {
                this.AddAttribute(att);
                output = true;
            }
            else if (att is XmlNumericFormatAttribute)
            {
                this.AddAttribute(att);
                output = true;
            }
            else if (att is XmlOrderAttribute)
            {
                this.Order = ((XmlOrderAttribute)att).Order;
            }
            else if (att is XmlIncludeAttribute)
            {
                this.IncludeTypes.Add(((XmlIncludeAttribute)att).Type);
            }
            else if (att is XmlArrayAttribute)
            {
                this.DefaultElementName = ((XmlArrayAttribute)att).ElementName;
                output = true;
            }
            else if (att is XmlInlineArrayAttributeAttribute)
            {
                XmlInlineArrayAttributeAttribute a = att as XmlInlineArrayAttributeAttribute;

                if (a.Type == null)
                {
                    this.DefaultAttributeName = a.AttributeName;
                    this.DefaultElementName   = null;

                    if (!string.IsNullOrEmpty(a.ItemSeparator))
                    {
                        this.AddAttribute(a);
                    }
                }
                else
                {
                    Type itemType = a.Type.GetElementType();
                    if (itemType == null)
                    {
                        throw new Exception("No es posible inferir el tipo del item del array de la propiedad " + this.PropertyName + " del tipo " + this.OwnerType.Name);
                    }

                    if (this.PropertyType.IsArray)
                    {
                        Type dcItemType = this.PropertyType.GetElementType();
                        if (dcItemType == null || !dcItemType.Equals(itemType))
                        {
                            throw new Exception("El tipo del item del array declarado en el atributo XmlInlineArrayAttributeAttribute es distinto al tipo del item del array de la propiedad " + this.PropertyName + " del tipo " + this.OwnerType.Name);
                        }
                    }

                    //agrego el tipo de el arrar..
                    this.AddAttributeToTypeMap(a.AttributeName, a.Type);
                    this.AddTypeToAttributeMap(a.Type, a.AttributeName);

                    //agrego el tipo del item
                    this.AddTypeToAttributeItemMap(itemType, a.AttributeName);
                    this.AddAttributeToTypeItemMap(a.AttributeName, itemType);

                    //si el tipo del array es object[], los item son por ej int, detecta que no es inline, pero al serializaer los items va
                    //a detectar que cada item se tiene que serializar como attributo, pero no tiene elemento definido.....
                    //this.AddTypeToElementItemMap(a.Type.GetElementType(), a.AttributeName);
                    //this.AddElementToTypeItemMap(a.AttributeName, a.Type.GetElementType());

                    //el separador lo busco en la lista de attributos
                    this.AddAttribute(att);

                    if (!this.IncludeTypes.Contains(a.Type))
                    {
                        this.AddIncludeTypes(a.Type);
                    }
                }

                output = true;
            }
            else if (att is XmlInlineArrayElementAttribute)
            {
                XmlInlineArrayElementAttribute a = att as XmlInlineArrayElementAttribute;

                if (a.Type == null)
                {
                    this.DefaultAttributeName = null;
                    this.DefaultElementName   = a.ElementName;

                    if (!string.IsNullOrEmpty(a.ItemSeparator))
                    {
                        this.AddAttribute(a);
                    }
                }
                else
                {
                    //agrego el tipo del item
                    this.AddTypeToElementItemMap(a.Type.GetElementType(), a.ElementName);
                    this.AddElementToTypeItemMap(a.ElementName, a.Type.GetElementType());

                    //agrego el tipo de el arrar..
                    this.AddElementToTypeMap(a.ElementName, a.Type);
                    this.AddTypeToElementMap(a.Type, a.ElementName);

                    //el separador lo busco en la lista de attributos
                    this.AddAttribute(att);

                    if (!this.IncludeTypes.Contains(a.Type))
                    {
                        this.AddIncludeTypes(a.Type);
                    }
                }
                output = true;
            }
            else if (att is XmlArrayItemValueAttribute)
            {
                XmlArrayItemValueAttribute a = att as XmlArrayItemValueAttribute;
                if (a.Type == null)
                {
                    this.DefaulItemAttributeName = a.AttributeName;
                    this.DefaultItemElementName  = a.ElementName;
                }
                else
                {
                    this.AddTypeToElementItemMap(a.Type, a.ElementName);
                    this.AddElementToTypeItemMap(a.ElementName, a.Type);

                    if (TypeHelper.IsValueType(a.Type))
                    {
                        this.AddTypeToAttributeItemMap(a.Type, a.AttributeName);
                        this.AddAttributeToTypeItemMap(a.AttributeName, a.Type);
                    }

                    if (!this.IncludeTypes.Contains(a.Type))
                    {
                        this.AddIncludeTypes(a.Type);
                    }
                }
                output = true;
            }
            else if (att is XmlArrayItemAttribute)
            {
                XmlArrayItemAttribute a = att as XmlArrayItemAttribute;
                if (a.Type == null)
                {
                    this.DefaultItemElementName = a.ElementName;
                }
                else
                {
                    this.AddTypeToElementItemMap(a.Type, a.ElementName);
                    this.AddElementToTypeItemMap(a.ElementName, a.Type);

                    if (!this.IncludeTypes.Contains(a.Type))
                    {
                        this.AddIncludeTypes(a.Type);
                    }
                }
                output = true;
            }
            else if (att is XmlDictionaryKeyElementAttribute)
            {
                XmlDictionaryKeyElementAttribute a = att as XmlDictionaryKeyElementAttribute;
                if (a.Type == null)
                {
                    this.DefaultDictionaryKeyElementName = a.ElementName;
                }
                else
                {
                    this.AddElementToTypeDictionaryKeyMap(a.ElementName, a.Type);
                    this.AddTypeToElementDictionaryKeyMap(a.Type, a.ElementName);

                    if (!this.IncludeTypes.Contains(a.Type))
                    {
                        this.AddIncludeTypes(a.Type);
                    }
                }
                output = true;
            }
            else if (att is XmlDictionaryValueElementAttribute)
            {
                XmlDictionaryValueElementAttribute a = att as XmlDictionaryValueElementAttribute;
                if (a.Type == null)
                {
                    this.DefaultDictionaryValueElementName = a.ElementName;
                }
                else
                {
                    this.AddElementToTypeDictionaryValueMap(a.ElementName, a.Type);
                    this.AddTypeToElementDictionaryValueMap(a.Type, a.ElementName);

                    if (!this.IncludeTypes.Contains(a.Type))
                    {
                        this.AddIncludeTypes(a.Type);
                    }
                }
                output = true;
            }
            else if (att is XmlDictionaryKeyAttributeAttribute)
            {
                XmlDictionaryKeyAttributeAttribute a = att as XmlDictionaryKeyAttributeAttribute;
                if (a.Type == null)
                {
                    Type declaringType = this.PropertyType;
                    Type keyType       = typeof(object);

                    if (declaringType.IsGenericType)
                    {
                        Type[] genercisTypes = declaringType.GetGenericArguments();
                        keyType = genercisTypes[0];
                    }

                    if (TypeHelper.IsValueType(keyType))
                    {
                        this.DefaultDictionaryKeyAttributeName = a.AttributeName;
                    }
                }
                else
                {
                    if (TypeHelper.IsValueType(a.Type))
                    {
                        this.AddAttributeToTypeDictionaryKeyMap(a.AttributeName, a.Type);
                        this.AddTypeToAttributeDictionaryKeyMap(a.Type, a.AttributeName);
                    }

                    if (!this.IncludeTypes.Contains(a.Type))
                    {
                        this.AddIncludeTypes(a.Type);
                    }
                }
                output = true;
            }
            else if (att is XmlDictionaryValueAttributeAttribute)
            {
                XmlDictionaryValueAttributeAttribute a = att as XmlDictionaryValueAttributeAttribute;
                if (a.Type == null)
                {
                    Type declaringType = this.PropertyType;
                    Type valueType     = typeof(object);

                    if (declaringType.IsGenericType)
                    {
                        Type[] genercisTypes = declaringType.GetGenericArguments();
                        valueType = genercisTypes[0];
                    }

                    if (TypeHelper.IsValueType(valueType))
                    {
                        this.DefaultDictionaryValueAttributeName = a.AttributeName;
                    }
                }
                else
                {
                    if (TypeHelper.IsValueType(a.Type))
                    {
                        this.AddAttributeToTypeDictionaryValueMap(a.AttributeName, a.Type);
                        this.AddTypeToAttributeDictionaryValueMap(a.Type, a.AttributeName);
                    }

                    if (!this.IncludeTypes.Contains(a.Type))
                    {
                        this.AddIncludeTypes(a.Type);
                    }
                }
                output = true;
            }
            else if (att is XmlDictionaryItemElementAttribute)
            {
                this.DictionaryItemElementName = ((XmlDictionaryItemElementAttribute)att).ElementName;
                output = true;
            }
            else if (att is XmlElementAttribute)
            {
                XmlElementAttribute atElem = (XmlElementAttribute)att;

                if (!string.IsNullOrEmpty(atElem.ElementName))
                {
                    if (atElem.Type != null)
                    {
                        this.AddElementToTypeMap(atElem.ElementName, atElem.Type);
                        this.AddTypeToElementMap(atElem.Type, atElem.ElementName);

                        if (!this.IncludeTypes.Contains(atElem.Type))
                        {
                            this.AddIncludeTypes(atElem.Type);
                        }
                    }
                    else
                    {
                        this.DefaultElementName = atElem.ElementName;
                    }
                }
                else
                {
                    this.DefaultElementName = this.PropertyName;
                }

                output = true;
            }
            else if (att is XmlAttributeAttribute)
            {
                XmlAttributeAttribute atElem = (XmlAttributeAttribute)att;

                if (!string.IsNullOrEmpty(atElem.AttributeName))
                {
                    if (atElem.Type != null)
                    {
                        if (TypeHelper.IsValueType(atElem.Type))
                        {
                            this.AddAttributeToTypeMap(atElem.AttributeName, atElem.Type);
                            this.AddTypeToAttributeMap(atElem.Type, atElem.AttributeName);
                        }

                        if (!this.IncludeTypes.Contains(atElem.Type))
                        {
                            this.AddIncludeTypes(atElem.Type);
                        }
                    }
                    else
                    {
                        if (TypeHelper.IsValueType(this.PropertyType))
                        {
                            this.DefaultAttributeName = atElem.AttributeName;
                        }
                    }
                }
                else
                {
                    this.DefaultAttributeName = this.PropertyName;
                }

                output = true;
            }
            else
            {
                this.AddAttribute(att as Attribute);
            }

            return(output);
        }