protected virtual void InitializeObjectType() { if (this.IsSerializable) { object att; object[] atts = this.Type.GetCustomAttributes(false); for (int i = 0; i < atts.Length; i++) { att = atts[i]; if (att is XmlRootAttribute) { this.ElementName = ((XmlRootAttribute)atts[0]).ElementName; } else if (att is SerializableAttribute) { this.IsSerializable = true; } else if (att is XmlElementValueAttribute) { XmlElementValueAttribute e = (XmlElementValueAttribute)att; this.ElementName = e.ElementName; if (TypeHelper.IsValueType(this.Type)) { this.AttributeName = e.AttributeName; } } else { this.Attributes.Add(att as Attribute); } } object[] atts1 = this.Type.GetCustomAttributes(typeof(XmlIncludeAttribute), true); foreach (XmlIncludeAttribute at in atts1) { this.IncludeTypes.Add(at.Type); } //cargo la propiedades PropertyInfo info; PropertyInfo[] infos = this.Type.GetProperties(__flags); PropertyDescriptor prop; for (int i = 0; i < infos.Length; i++) { info = infos[i]; Type t = info.PropertyType; if ((TypeHelper.IsSerializable(t) || t.IsInterface) && info.CanRead && info.CanWrite) { try { object[] ignoreAtt = info.GetCustomAttributes(typeof(XmlIgnoreAttribute), false); if (ignoreAtt.Length == 0) { if (t.IsArray || TypeHelper.IsListType(t)) { prop = new ListPropertyDescriptor(info); } else if (TypeHelper.IsDictionaryType(t)) { prop = new DictionaryPropertyDescriptor(info); } else if (TypeHelper.IsValueType(t)) { prop = new ValueTypePropertyDescriptor(info); } else { prop = new ObjectPropertyDescriptor(info); } //si la propiedad es serializable, la agrego...sino no if (prop.Metadata.IsSerializable) { this.Properties.Add(prop); this.PropertyMap.Add(prop.Metadata.PropertyName, prop); } } } catch (Exception ex) { throw new Exception("Error al intentar obtener la descripción de la propiedad " + info.Name + " del tipo " + this.Type.FullName, ex); } } } this.Properties = this.Properties.OrderBy(p => p.Metadata.Order).ToList(); } }
/// <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); }