Esempio n. 1
1
        private XElement WriteValue(string elementName, SerializeAttribute serializeAttribute, object value, params object[] content)
        {
            if (value == null) throw new ArgumentNullException("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;
        }
Esempio n. 2
0
        private XElement CreateElement(string elementName, object value, SerializeAttribute attribute, params object[] content)
        {
            Type type = value.GetType();

            var result = new XElement(elementName, content);

            result.Add(new XAttribute(XmlConstants.TypeId, GetTypeId(type).ToString()));

            if (attribute != null)
            {
                if (attribute.ConstructorArgumentIndex > -1)
                {
                    result.Add(new XAttribute(XmlConstants.Arg, attribute.ConstructorArgumentIndex));
                    result.Add(new XAttribute(XmlConstants.UseCase, attribute.UseCase));
                }

                if (!string.IsNullOrWhiteSpace(attribute.StaticConstructorMethodName))
                {
                    result.Add(new XAttribute(XmlConstants.StaticConstructorMethod, attribute.StaticConstructorMethodName));
                }

                if (attribute.UseStaticConstructor)
                {
                    result.Add(new XAttribute(XmlConstants.StaticMethodIndicator, attribute.UseStaticConstructor));
                }
            }

            if (!type.IsValueType && !type.Equals(typeof(string))) // Don't cache value types or strings
            {
                _visitedObjects.Add(value, result);
            }

            return(result);
        }
Esempio n. 3
0
        private XElement CreateElement(string elementName, object value, SerializeAttribute attribute, params object[] content)
        {
            Type type = value.GetType();

            var result = new XElement(elementName, content);

            result.Add(new XAttribute(XmlConstants.TYPE_ID, GetTypeID(type).ToString()));

            if (attribute != null && attribute.ConstructorArgumentIndex >= 0)
            {
                result.Add(new XAttribute(XmlConstants.ARG, attribute.ConstructorArgumentIndex));
            }

            if (!type.IsValueType && !type.Equals(typeof(string))) // Don't cache value types or strings
            {
                _visitedObjects.Add(value, result);
            }

            return(result);
        }
 /// <summary>
 /// Creates a new SerializationMapEntry instance.
 /// </summary>
 /// <param name="memberInfo">Info about the field or property to serialize.</param>
 /// <param name="attribute">Details about how to serialize this member.</param>
 public SerializationMapEntry(MemberInfo memberInfo, SerializeAttribute attribute)
 {
     Member = memberInfo;
     Attribute = attribute;
 }
Esempio n. 5
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);
        }
Esempio n. 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SerializationMapEntry"/> class.
 /// </summary>
 /// <param name="memberInfo">Info about the field or property to serialize.</param>
 /// <param name="attribute">Details about how to serialize this member.</param>
 public SerializationMapEntry(MemberInfo memberInfo, SerializeAttribute attribute)
 {
     Member    = memberInfo;
     Attribute = attribute;
 }
Esempio n. 7
0
        private XElement CreateElement(string elementName, object value, SerializeAttribute attribute, params object[] content)
        {
            Type type = value.GetType();

            var result = new XElement(elementName, content);
            result.Add(new XAttribute(XmlConstants.TYPE_ID, GetTypeID(type).ToString()));

            if (attribute != null && attribute.ConstructorArgumentIndex >= 0)
                result.Add(new XAttribute(XmlConstants.ARG, attribute.ConstructorArgumentIndex));

            if (!type.IsValueType && !type.Equals(typeof(string))) // Don't cache value types or strings
                _visitedObjects.Add(value, result);

            return result;
        }