Beispiel #1
0
        protected virtual XAttribute CreateTypeAttribute(Type declareType, Type actualType,
                                                         TypeAttributeHandling attributeHandling, FormatterAssemblyStyle?assemblyNameStyle)
        {
            XAttribute typeAttribute = null;

            switch (attributeHandling)
            {
            case TypeAttributeHandling.Auto:
                if (actualType != declareType)
                {
                    typeAttribute = XConvertUtil.CreateTypeAttribute(actualType, assemblyNameStyle);
                }
                break;

            case TypeAttributeHandling.Objects:
                if (!actualType.IsValueType)
                {
                    typeAttribute = XConvertUtil.CreateTypeAttribute(actualType, assemblyNameStyle);
                }
                break;

            case TypeAttributeHandling.All:
                typeAttribute = XConvertUtil.CreateTypeAttribute(actualType, assemblyNameStyle);
                break;
            }
            return(typeAttribute);
        }
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
 protected override void PopulateElementValue(
     XElement element, Type declareType, object obj, XConvertSettings settings)
 {
     element.Value = obj != null
         ? XConvertUtil.TypeNameOf((Type)obj, settings.AssemblyNameStyle)
         : null;
 }
Beispiel #4
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 #5
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 #6
0
        protected virtual void ToObjectArgumentsCheck(Type objType, XElement element)
        {
            Ensure.Argument.NotNull(element, "element");
            Ensure.Argument.NotNull(objType, "objType");

            var savedType = XConvertUtil.ParseType(element);

            if (savedType != null)
            {
                Ensure.Argument.Is(objType, "objType", savedType);
                EnsureArgumentCanConvert(savedType, "element");
            }
            else
            {
                EnsureArgumentCanConvert(objType, "objType");
            }
        }
Beispiel #7
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);
            }
        }
 protected override void PopulateElementAttributes(
     XElement element, Type declareType, object obj, XConvertSettings settings)
 {
     element.Add(XConvertUtil.CreateTypeAttribute(declareType, settings.AssemblyNameStyle));
 }