Example #1
0
        private void FillObject(XElement element, object parent, bool includeConstructorElements)
        {
            Type type = parent.GetType();
            var  map  = SerializationMap.FromType(type);

            var idAttribute = element.Attribute(XmlConstants.ID);

            if (idAttribute != null)
            {
                _references[idAttribute.Value] = parent;
            }

            // Populate the rest of the members
            var nonConstructorArgElements = from m in element.Elements(XmlConstants.MEMBER)
                                            where m.Attribute(XmlConstants.ARG) == null || includeConstructorElements
                                            select m;

            foreach (var member in nonConstructorArgElements)
            {
                string name     = GetName(member);
                var    mapEntry = map.Members.FirstOrDefault(m => m.Attribute.Name == name);
                if (mapEntry == null)
                {
                    continue; // XML is probably out of date with the code if this happens
                }
                if (mapEntry.Member is PropertyInfo)
                {
                    PropertyInfo propertyInfo = (PropertyInfo)mapEntry.Member;
                    if (propertyInfo.CanWrite)
                    {
                        propertyInfo.SetValue(parent, ReadObject(member, null), null);
                    }
                    else
                    {
                        FillObject(member, propertyInfo.GetValue(parent, null), false);
                    }
                }
                else
                {
                    ((FieldInfo)mapEntry.Member).SetValue(parent, ReadObject(member, null));
                }
            }
            if (type.IsArray)
            {
                PopulateArray(element, (Array)parent);
            }
            else if (typeof(IDictionary).IsAssignableFrom(type))
            {
                PopulateDictionary(element, (IDictionary)parent);
            }
            else if (typeof(IList).IsAssignableFrom(type))
            {
                PopulateList(element, (IList)parent);
            }
        }
Example #2
0
        private XElement WriteValue(string elementName, SerializeAttribute serializeAttribute, object value, params object[] content)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            XElement element;

            Type type  = value.GetType();
            int  refId = _visitedObjects.FindRefId(value);

            if (refId >= 0)
            {
                element = new XElement(elementName, content, new XAttribute(XmlConstants.Ref, refId.ToString(CultureInfo.InvariantCulture)));
                if (serializeAttribute != null && serializeAttribute.ConstructorArgumentIndex >= 0)
                {
                    element.Add(new XAttribute(XmlConstants.Arg, serializeAttribute.ConstructorArgumentIndex));
                }
                return(element);
            }

            element = CreateElement(elementName, value, serializeAttribute, content);
            if (serializeAttribute != null && serializeAttribute.Formatter != null)
            {
                element.Add(GetFormattedValue(serializeAttribute.Formatter, value));
            }
            else if (type.IsPrimitive)
            {
                element.Add(new XAttribute(XmlConstants.Value, Convert.ToString(value, CultureInfo.InvariantCulture)));
            }
            else if (type.IsEnum)
            {
                element.Add(new XAttribute(XmlConstants.Value, value.ToString()));
            }
            else if (value is string)
            {
                element.Add(new XAttribute(XmlConstants.Value, XmlHelper.EscapeInvalidCharacters((string)value)));
            }
            else if (typeof(IDictionary).IsAssignableFrom(type))
            {
                WriteDictionaryElements(element, (IDictionary)value);
            }
            else if (typeof(IList).IsAssignableFrom(type) || typeof(ICollection).IsAssignableFrom(type))
            {
                WriteListElements(element, (IEnumerable)value);
            }
            else if (type == typeof(DateTime))
            {
                element.Add(new XAttribute(XmlConstants.Value, Convert.ToString(value, CultureInfo.InvariantCulture)));
            }
            else if (type == typeof(Color))
            {
                element.Add(new XAttribute(XmlConstants.Value, ColorTranslator.ToHtml((Color)value)));
            }
            else if (type == typeof(PointF))
            {
                PointF p = (PointF)value;
                element.Add(new XAttribute(XmlConstants.Value, XmlHelper.EscapeInvalidCharacters(p.X + "|" + p.Y)));
            }
            else
            {
                var map = SerializationMap.FromType(value.GetType());
                WriteMembers(element, map.Members, value);
            }

            return(element);
        }