Beispiel #1
0
        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);
        }
Beispiel #2
0
        ObjectDeserializer CreateElementDeserializer(PropertyInfo property, Type type, XmlArrayItemAttribute arrayItemAttribute)
        {
            if (!property.CanWrite)
            {
                // TODO throw
            }

            var icollection = type.GetInterface("ICollection`1");

            if (icollection == null)
            {
                // TODO throw
            }

            var add               = icollection.GetMethod("Add");
            var item_type         = GetType(arrayItemAttribute.Type, icollection.GetGenericArguments()[0]);
            var item_deserializer = GetDeserializer(item_type);

            return((obj, context) => {
                var collection = Activator.CreateInstance(type);
                var depth = context.Reader.Depth;
                while (context.Reader.Read() && context.Reader.NodeType == XmlNodeType.Element && context.Reader.Depth > depth)
                {
                    var item_reader = context.Reader.ReadSubtree();
                    item_reader.Read();
                    try {
                        add.Invoke(collection, new object [] { item_deserializer(new XmlDeserializationContext(this, item_reader)) });
                    } catch {
                    } finally {
                        item_reader.Close();
                    }
                }
            });
        }
 bool ProcessArrayItemAttribute (PropertyInfo propertyInfo, XmlArrayItemAttribute attribute)
 {
     if (attribute != null) {
         var name = string.IsNullOrEmpty (attribute.Name) ? propertyInfo.Name : attribute.Name;
         var id = PropertyName.CreateForElement (name, attribute.Prefix);
         var property = new EnumerationPropertyVisitor (propertyInfo);
         ProcessNestedAttributes (id, property);
         properties[id] = property;
         return true;
     } else {
         return false;
     }
 }