Beispiel #1
0
 protected override void PopulateElementValue(
     XElement element, Type declareType, object obj, XConvertSettings settings)
 {
     element.Value = obj != null
         ? XConvertUtil.TypeNameOf((Type)obj, settings.AssemblyNameStyle)
         : null;
 }
Beispiel #2
0
        protected override void PopulateElementChildren(
            XElement element, Type declareType, object obj, XConvertSettings settings)
        {
            if (obj == null)
            {
                return;
            }

            var collectionProxy = CreateCollectionProxy(obj);
            var itemDeclareType = collectionProxy.ItemType;

            foreach (var item in collectionProxy)
            {
                var itemActualType = item != null?item.GetType() : null;

                var usingItemType = itemActualType ?? itemDeclareType;

                var itemElement = XConvert.ToElement(ItemName, item, settings);
                if (itemDeclareType != usingItemType)
                {
                    itemElement.Add(XConvertUtil.CreateTypeAttribute(usingItemType, settings.AssemblyNameStyle));
                }
                element.Add(itemElement);
            }
        }
Beispiel #3
0
        /// <summary>
        ///     Assume specified xml element represents an object, convert the xml element to the object instance.
        /// </summary>
        /// <param name="element">The xml element that contains the contents of the object.</param>
        /// <param name="settings">
        ///     Convert settings used during the convert. <see cref="XConvertSettings.Default" /> is used if the value is
        ///     <see langword="null" />.
        /// </param>
        /// <returns>Object that converted from <paramref name="element" />.</returns>
        public object ToObject(XElement element, XConvertSettings settings)
        {
            Ensure.Argument.NotNull(element, "element");

            var objType = XConvertUtil.ParseType(element);

            Ensure.Argument.NotNull(objType, "element", "Object type not found.");

            return(ToObject(objType, element, settings));
        }
Beispiel #4
0
        public static void PopulateObject(object obj, XElement element, XConvertSettings settings = null)
        {
            Ensure.Argument.NotNull(obj, "obj");
            Ensure.Argument.NotNull(element, "element");

            settings = settings ?? XConvertSettings.Default;

            var converter = settings.FindConverter(obj.GetType());

            converter.PopulateObject(obj, element, settings);
        }
Beispiel #5
0
        /// <summary>
        ///     Convert xml element to its representing object.
        /// </summary>
        /// <param name="objType">Type of the result object.</param>
        /// <param name="element">The xml elemnt to convert.</param>
        /// <param name="settings">
        ///     Convert settings used during the convertion. <see cref="XConvertSettings.Default" /> will be used if the value is
        ///     <see langword="null" />.
        /// </param>
        /// <returns>Object that converted from <paramref name="element" />.</returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="objType" /> or <paramref name="element" /> is <see langword="null" />.
        /// </exception>
        public static object ToObject(Type objType, XElement element, XConvertSettings settings = null)
        {
            Ensure.Argument.NotNull(objType, "objType");
            Ensure.Argument.NotNull(element, "element");

            settings = settings ?? XConvertSettings.Default;

            var converter = settings.FindConverter(objType);

            return(converter.ToObject(objType, element, settings));
        }
Beispiel #6
0
        /// <summary>
        ///     Convert an object to xml element.
        /// </summary>
        /// <param name="name">Name of the element.</param>
        /// <param name="obj">The object to convert.</param>
        /// <param name="settings">
        ///     Convert settings used during the convertion. <see cref="XConvertSettings.Default" /> will be used if the value is
        ///     <see langword="null" />.
        /// </param>
        /// <returns>An instance of <see cref="XElement" /> that represents <paramref name="obj" />.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="obj" /> is <see langword="null" />.</exception>
        public static XElement ToElement(string name, object obj, XConvertSettings settings = null)
        {
            Ensure.Argument.NotNull(obj, "obj");

            settings = settings ?? XConvertSettings.Default;

            var type      = obj.GetType();
            var converter = settings.FindConverter(type);

            return(converter.ToElement(name, type, obj, settings));
        }
Beispiel #7
0
        protected override void PopulateObjectImpl(object obj, XElement element, XConvertSettings settings)
        {
            var collectionProxy = CreateCollectionProxy(obj);

            foreach (var itemElement in element.Elements(ItemName))
            {
                var itemType = XConvertUtil.ParseType(itemElement) ?? collectionProxy.ItemType;
                var item     = XConvert.ToObject(itemType, itemElement, settings);
                collectionProxy.Add(item);
            }
        }
Beispiel #8
0
        protected override void PopulateElementChildren(
            XElement element, Type declareType, object obj, XConvertSettings settings)
        {
            if (obj == null)
            {
                return;
            }

            var actualType = obj.GetType();

            Ensure.Argument.Is(actualType, "obj", declareType);

            foreach (var members in EnumMembers(obj))
            {
                var memberCount = members.Count();
                foreach (var member in members)
                {
                    Type   declareMemberType = null;
                    Type   actualMemberType  = null;
                    object value             = null;

                    var field = member as FieldInfo;
                    if (field != null)
                    {
                        declareMemberType = field.FieldType;
                        value             = field.GetValue(obj);
                        if (value != null)
                        {
                            actualMemberType = value.GetType();
                        }
                    }

                    var property = member as PropertyInfo;
                    if (property != null && property.CanRead)
                    {
                        declareMemberType = property.PropertyType;
                        value             = property.GetValue(obj, null);
                        if (value != null)
                        {
                            actualMemberType = value.GetType();
                        }
                    }

                    var memberType = actualMemberType ?? declareMemberType;
                    var converter  = settings.FindConverter(memberType);

                    var name          = memberCount > 1 ? GetHierachicalMemberName(actualType, member) : member.Name;
                    var memberElement = converter.ToElement(name, declareMemberType, value, settings);
                    element.Add(memberElement);
                }
            }
        }
Beispiel #9
0
        /// <summary>
        ///     Populate the contents of specified xml element to an object.
        /// </summary>
        /// <param name="obj">The object to populate.</param>
        /// <param name="element">The xml element that contains the contents of the object to populate.</param>
        /// <param name="settings">
        ///     Convert settings used during the convert. <see cref="XConvertSettings.Default" /> is used if the value is
        ///     <see langword="null" />.
        /// </param>
        public void PopulateObject(object obj, XElement element, XConvertSettings settings)
        {
            Ensure.Argument.NotNull(obj, "obj");
            ToObjectArgumentsCheck(obj.GetType(), element);

            settings = settings ?? XConvertSettings.Default;

            var objType = obj.GetType();

            ExecuteCallback <OnConvertingToObjectAttribute>(objType, obj);
            PopulateObjectImpl(obj, element, settings);
            ExecuteCallback <OnConvertedToObjectAttribute>(objType, obj);
        }
Beispiel #10
0
        /// <summary>
        ///     Populate contents of specified <see cref="XElement" /> instance from specified object.
        /// </summary>
        /// <param name="element">The <see cref="XElement" /> instance to populate.</param>
        /// <param name="declareType">Declaring type of the specified object.</param>
        /// <param name="obj">The object that contains the contents to populate from.</param>
        /// <param name="settings">
        ///     Convert settings used when populating. <see cref="XConvertSettings.Default" /> will be used if the value is
        ///     <see langword="null" />.
        /// </param>
        public void PopulateElement(XElement element, Type declareType, object obj, XConvertSettings settings)
        {
            Ensure.Argument.NotNull(element, "element");
            ToElementArgumentsCheck(declareType, obj);

            settings = settings ?? XConvertSettings.Default;

            PopulateElementAttributes(element, declareType, obj, settings);
            PopulateElementValue(element, declareType, obj, settings);
            PopulateElementChildren(element, declareType, obj, settings);

            ExecuteCallback <OnConvertedToElementAttribute>(declareType, obj);
        }
Beispiel #11
0
        protected virtual void PopulateElementAttributes(
            XElement element, Type declareType, object obj, XConvertSettings settings)
        {
            var actualType = obj != null?obj.GetType() : declareType;

            var typeAttribute = CreateTypeAttribute(declareType, actualType,
                                                    settings.TypeAttributeHandling, settings.AssemblyNameStyle);

            if (typeAttribute != null)
            {
                element.Add(typeAttribute);
            }
        }
Beispiel #12
0
        public static XDocument ToDocument(string name, object obj, XConvertSettings settings = null)
        {
            Ensure.Argument.NotNullOrEmpty(name, "name");
            Ensure.Argument.NotNull(obj, "obj");

            var document = new XDocument();

            var element = ToElement(name, obj, settings);

            document.Add(element);

            return(document);
        }
Beispiel #13
0
        public static XDocument ToDocument(
            IEnumerable <KeyValuePair <string, object> > objects, XConvertSettings settings = null)
        {
            Ensure.Argument.NotNullOrEmpty(objects, "objects");

            var document = new XDocument();

            foreach (var kvp in objects)
            {
                document.Add(ToElement(kvp.Key, kvp.Value, settings));
            }

            return(document);
        }
Beispiel #14
0
        /// <summary>
        ///     Convert specified object to <see cref="XElement" />.
        /// </summary>
        /// <param name="name">Name of the converted <see cref="XElement" />.</param>
        /// <param name="declareType">Declare type of the converting object.</param>
        /// <param name="obj">Object to convert.</param>
        /// <param name="settings">
        ///     Convert settings used during the convertion. <see cref="XConvertSettings.Default" /> will be used if the value is
        ///     <see langword="null" />.
        /// </param>
        /// <returns>A <see cref="XElement" /> instance converted from <paramref name="obj" />.</returns>
        public XElement ToElement(string name, Type declareType, object obj, XConvertSettings settings)
        {
            Ensure.Argument.NotNullOrEmpty(name, "name");
            ToElementArgumentsCheck(declareType, obj);

            settings = settings ?? XConvertSettings.Default;

            ExecuteCallback <OnConvertingToElementAttribute>(declareType, obj);

            var element = new XElement(name);

            PopulateElement(element, declareType, obj, settings);
            return(element);
        }
Beispiel #15
0
        /// <summary>
        ///     Create object of specified type, then populate contents of specified xml element to the created object.
        /// </summary>
        /// <param name="objType">The type of object to create.</param>
        /// <param name="element">The xml element that contains the contents to populate.</param>
        /// <param name="settings">
        ///     Convert settings used during the convert. <see cref="XConvertSettings.Default" /> is used if the value is
        ///     <see langword="null" />.
        /// </param>
        /// <returns>Object of type <paramref name="objType" /> that holds contents of <paramref name="element" />.</returns>
        public object ToObject(Type objType, XElement element, XConvertSettings settings)
        {
            ToObjectArgumentsCheck(objType, element);

            settings = settings ?? XConvertSettings.Default;

            var obj = CreateObject(objType, element, settings);

            ExecuteCallback <OnConvertingToObjectAttribute>(objType, obj);
            if (obj != null)
            {
                PopulateObjectImpl(obj, element, settings);
            }
            ExecuteCallback <OnConvertedToObjectAttribute>(objType, obj);
            return(obj);
        }
Beispiel #16
0
        protected override void PopulateObjectImpl(object obj, XElement element, XConvertSettings settings)
        {
            var objType     = obj.GetType();
            var membersDict = GetMembers(obj);

            foreach (var memberElement in element.Elements())
            {
                string     name;
                MemberInfo member;
                GetMemberInfo(objType, membersDict, memberElement, out name, out member);
                if (member == null)
                {
                    switch (MissingMemberHandling)
                    {
                    case MissingMemberHandling.Ignore:
                        continue;

                    case MissingMemberHandling.ThrowException:
                        throw new MissingMemberException(objType.FullName, name);

                    default:
                        continue;
                    }
                }

                var declareMemberType = GetDeclareMemberType(member);
                var actualMemberType  = XConvertUtil.ParseType(memberElement);
                if (actualMemberType != null)
                {
                    if (actualMemberType != declareMemberType && !actualMemberType.IsSubclassOf(declareMemberType))
                    {
                        throw new XmlException(string.Format(
                                                   "Object(of type {0}) member type({1}) from xml does not match with its declare type({2}) of the object.",
                                                   objType, actualMemberType, declareMemberType));
                    }
                }

                var usingMemberType = actualMemberType ?? declareMemberType;
                var converter       = settings.FindConverter(usingMemberType);
                var value           = converter.ToObject(usingMemberType, memberElement, settings);
                SetMemberValue(member, obj, value);
            }
        }
Beispiel #17
0
 protected override object CreateObject(Type objType, XElement element, XConvertSettings settings)
 {
     return(ParseValue(element.Value));
 }
Beispiel #18
0
 protected virtual void PopulateElementValue(
     XElement element, Type declareType, object obj, XConvertSettings settings)
 {
     element.Value = obj != null?obj.ToString() : null;
 }
 protected override void PopulateElementAttributes(
     XElement element, Type declareType, object obj, XConvertSettings settings)
 {
     element.Add(XConvertUtil.CreateTypeAttribute(declareType, settings.AssemblyNameStyle));
 }
Beispiel #20
0
 protected virtual void PopulateObjectImpl(object obj, XElement element, XConvertSettings settings)
 {
 }
Beispiel #21
0
 protected virtual object CreateObject(Type objType, XElement element, XConvertSettings settings)
 {
     return(element.IsEmpty && !element.Elements().Any() ? null : Activator.CreateInstance(objType, true));
 }
 protected override object CreateObject(Type objType, XElement element, XConvertSettings settings)
 {
     return(element.IsEmpty ? null : element.Value);
 }
Beispiel #23
0
 protected override void PopulateElementValue(
     XElement element, Type declareType, object obj, XConvertSettings settings)
 {
 }
Beispiel #24
0
 public T ToObject <T>(XElement element, XConvertSettings settings)
 {
     return((T)ToObject(typeof(T), element, settings));
 }
Beispiel #25
0
 protected virtual void PopulateElementChildren(
     XElement element, Type declareType, object obj, XConvertSettings settings)
 {
 }
Beispiel #26
0
 public static object[] ToObjects(XDocument document, XConvertSettings settings = null)
 {
     Ensure.Argument.NotNull(document, "document");
     return(document.Elements().Select(e => ToObject(e, settings)).ToArray());
 }