static Serializer CreateSerializer(PropertyInfo property, XmlFlagAttribute flagAttribute) { if (property.PropertyType != typeof(bool)) { // TODO throw } var name = flagAttribute.Name ?? property.Name; var @namespace = flagAttribute.Namespace; return((obj, context) => { if ((bool)obj) { context.Writer.WriteStartElement(name, @namespace); context.Writer.WriteEndElement(); } }); }
void ProcessMember(PropertyInfo property, List <Serializer> attributeSerializers, List <Serializer> elementSerializers) { XmlAttributeAttribute attribute_attribute = null; XmlElementAttribute element_attribute = null; XmlFlagAttribute flag_attribute = null; XmlArrayAttribute array_attribute = null; XmlArrayItemAttribute array_item_attribute = null; foreach (var custom_attribute in property.GetCustomAttributes(false)) { var attribute = custom_attribute as XmlAttributeAttribute; if (attribute != null) { attribute_attribute = attribute; continue; } var element = custom_attribute as XmlElementAttribute; if (element != null) { element_attribute = element; continue; } var flag = custom_attribute as XmlFlagAttribute; if (flag != null) { flag_attribute = flag; continue; } var array = custom_attribute as XmlArrayAttribute; if (array != null) { array_attribute = array; continue; } var array_item = custom_attribute as XmlArrayItemAttribute; if (array_item != null) { array_item_attribute = array_item; continue; } } if (attribute_attribute != null) { attributeSerializers.Add(CreateSerializer(property, attribute_attribute)); return; } if (element_attribute != null) { elementSerializers.Add(CreateSerializer(property, element_attribute)); return; } if (flag_attribute != null) { elementSerializers.Add(CreateSerializer(property, flag_attribute)); return; } if (array_attribute != null) { elementSerializers.Add(CreateSerializer(property, array_attribute, array_item_attribute)); return; } }
Dictionary <string, ObjectDeserializer> CreateElementAutoDeserializers(Type type, Deserializers deserializers) { var object_deserializers = new Dictionary <string, ObjectDeserializer> (); foreach (var property in type.GetProperties()) { XmlElementAttribute element_attribute = null; XmlFlagAttribute flag_attribute = null; XmlArrayAttribute array_attribute = null; XmlArrayItemAttribute array_item_attribute = null; foreach (var custom_attribute in property.GetCustomAttributes(false)) { var element = custom_attribute as XmlElementAttribute; if (element != null) { element_attribute = element; continue; } var flag = custom_attribute as XmlFlagAttribute; if (flag != null) { flag_attribute = flag; continue; } var array = custom_attribute as XmlArrayAttribute; if (array != null) { array_attribute = array; continue; } var array_item = custom_attribute as XmlArrayItemAttribute; if (array_item != null) { array_item_attribute = array_item; continue; } } if (element_attribute != null) { var deserialization_type = GetType(element_attribute.Type, property.PropertyType); var deserializer = CreateCustomDeserializer(property, deserialization_type, deserializers) ?? CreateElementDeserializer(property, deserialization_type); AddDeserializer(object_deserializers, CreateName(property.Name, element_attribute.Name, element_attribute.Namespace), deserializer); continue; } if (flag_attribute != null) { AddDeserializer(object_deserializers, CreateName(property.Name, flag_attribute.Name, flag_attribute.Namespace), CreateElementDeserializer(property)); continue; } if (array_attribute != null) { AddDeserializer(object_deserializers, CreateName(property.Name, array_attribute.Name, array_attribute.Namespace), CreateElementDeserializer(property, array_item_attribute)); continue; } } return(object_deserializers); }