Ejemplo n.º 1
0
        protected override object GetValueFromXml(XElement root, XName name, PropertyInfo prop)
        {
            bool isAttribute = false;

            //Check for the DeserializeAs attribute on the property
            DeserializeAsAttribute options = prop.GetAttribute <DeserializeAsAttribute>();

            if (options != null)
            {
                name        = options.Name ?? name;
                isAttribute = options.Attribute;
            }

            if (isAttribute)
            {
                XAttribute attributeVal = GetAttributeByName(root, name);

                if (attributeVal != null)
                {
                    return(attributeVal.Value);
                }
            }

            return(base.GetValueFromXml(root, name, prop));
        }
        private object HandleListDerivative(XElement root, string propName, Type type)
        {
            Type t = type.IsGenericType
                ? type.GetGenericArguments()[0]
                : type.BaseType.GetGenericArguments()[0];
            IList            list     = (IList)Activator.CreateInstance(type);
            IList <XElement> elements = root.Descendants(t.Name.AsNamespaced(this.Namespace))
                                        .ToList();
            string name = t.Name;
            DeserializeAsAttribute attribute = t.GetAttribute <DeserializeAsAttribute>();

            if (attribute != null)
            {
                name = attribute.Name;
            }

            if (!elements.Any())
            {
                XName lowerName = name.ToLower().AsNamespaced(this.Namespace);

                elements = root.Descendants(lowerName).ToList();
            }

            if (!elements.Any())
            {
                XName camelName = name.ToCamelCase(this.Culture).AsNamespaced(this.Namespace);

                elements = root.Descendants(camelName).ToList();
            }

            if (!elements.Any())
            {
                elements = root.Descendants()
                           .Where(e => e.Name.LocalName.RemoveUnderscoresAndDashes() == name)
                           .ToList();
            }

            if (!elements.Any())
            {
                XName lowerName = name.ToLower().AsNamespaced(this.Namespace);

                elements = root.Descendants()
                           .Where(e => e.Name.LocalName.RemoveUnderscoresAndDashes() == lowerName)
                           .ToList();
            }

            this.PopulateListFromElements(t, elements, list);

            // get properties too, not just list items
            // only if this isn't a generic type
            if (!type.IsGenericType)
            {
                this.Map(list, root.Element(propName.AsNamespaced(this.Namespace)) ?? root);
                // when using RootElement, the heirarchy is different
            }

            return(list);
        }
Ejemplo n.º 3
0
        private object Map(object target, IDictionary <string, object> data)
        {
            Type objType = target.GetType();
            List <PropertyInfo> props = objType.GetProperties()
                                        .Where(p => p.CanWrite)
                                        .ToList();

            foreach (PropertyInfo prop in props)
            {
                Type     type       = prop.PropertyType;
                object[] attributes = prop.GetCustomAttributes(typeof(DeserializeAsAttribute), false);
                string   name;

                if (attributes.Length > 0)
                {
                    DeserializeAsAttribute attribute = (DeserializeAsAttribute)attributes[0];
                    name = attribute.Name;
                }
                else
                {
                    name = prop.Name;
                }

                string[] parts = name.Split('.');
                IDictionary <string, object> currentData = data;
                object value = null;

                for (int i = 0; i < parts.Length; ++i)
                {
                    string actualName = parts[i].GetNameVariants(this.Culture)
                                        .FirstOrDefault(currentData.ContainsKey);

                    if (actualName == null)
                    {
                        break;
                    }

                    if (i == parts.Length - 1)
                    {
                        value = currentData[actualName];
                    }
                    else
                    {
                        currentData = (IDictionary <string, object>)currentData[actualName];
                    }
                }

                if (value != null)
                {
                    prop.SetValue(target, this.ConvertValue(type, value), null);
                }
            }

            return(target);
        }
Ejemplo n.º 4
0
        private object Map(object target, IDictionary <string, object> data)
        {
            Type type = target.GetType();
            List <PropertyInfo> list = (from p in type.GetProperties()
                                        where p.CanWrite
                                        select p).ToList();

            foreach (PropertyInfo item in list)
            {
                Type     propertyType     = item.PropertyType;
                object[] customAttributes = item.GetCustomAttributes(typeof(DeserializeAsAttribute), false);
                string   name;
                if (customAttributes.Length > 0)
                {
                    DeserializeAsAttribute deserializeAsAttribute = (DeserializeAsAttribute)customAttributes[0];
                    name = deserializeAsAttribute.Name;
                }
                else
                {
                    name = item.Name;
                }
                string[] array = name.Split('.');
                IDictionary <string, object> dictionary = data;
                object obj = null;
                for (int i = 0; i < array.Length; i++)
                {
                    IEnumerable <string>         nameVariants = array[i].GetNameVariants(Culture);
                    IDictionary <string, object> dictionary2  = dictionary;
                    string text = nameVariants.FirstOrDefault(dictionary2.ContainsKey);
                    if (text == null)
                    {
                        break;
                    }
                    if (i == array.Length - 1)
                    {
                        obj = dictionary[text];
                    }
                    else
                    {
                        dictionary = (IDictionary <string, object>)dictionary[text];
                    }
                }
                if (obj != null)
                {
                    item.SetValue(target, ConvertValue(propertyType, obj), null);
                }
            }
            return(target);
        }
        protected override object GetValueFromXml(XElement root, XName name, PropertyInfo prop)
        {
            bool flag = false;
            DeserializeAsAttribute attribute = prop.GetAttribute <DeserializeAsAttribute>();

            if (attribute != null)
            {
                string name2 = attribute.Name;
                name = ((name2 != null) ? ((XName)name2) : name);
                flag = attribute.Attribute;
            }
            if (flag)
            {
                XAttribute attributeByName = GetAttributeByName(root, name);
                if (attributeByName != null)
                {
                    return(attributeByName.Value);
                }
            }
            return(base.GetValueFromXml(root, name, prop));
        }
Ejemplo n.º 6
0
        protected virtual object Map(object x, XElement root)
        {
            Type objType = x.GetType();

            PropertyInfo[] props = objType.GetProperties();
            bool           deserializeFromContentAttributeAlreadyUsed = false;

            foreach (var prop in props)
            {
                var type         = prop.PropertyType.GetTypeInfo();
                var typeIsPublic = type.IsPublic || type.IsNestedPublic;

                if (!typeIsPublic || !prop.CanWrite)
                {
                    continue;
                }

                bool  deserializeFromContent   = false;
                bool  isNameDefinedInAttribute = false;
                XName name       = null;
                var   attributes = prop.GetCustomAttributes(typeof(DeserializeAsAttribute), false);

                if (attributes.Any())
                {
                    DeserializeAsAttribute attribute = (DeserializeAsAttribute)attributes.First();

                    name = attribute.Name.AsNamespaced(Namespace);
                    isNameDefinedInAttribute = !string.IsNullOrEmpty(name?.LocalName);

                    deserializeFromContent = attribute.Content;

                    if (deserializeFromContentAttributeAlreadyUsed && deserializeFromContent)
                    {
                        throw new ArgumentException("Class cannot have two properties marked with " +
                                                    "SerializeAs(Content = true) attribute.");
                    }

                    deserializeFromContentAttributeAlreadyUsed |= deserializeFromContent;
                }

                if (name == null)
                {
                    name = prop.Name.AsNamespaced(Namespace);
                }

                var value = GetValueFromXml(root, name, prop, isNameDefinedInAttribute);

                if (value == null)
                {
                    // special case for text content node
                    if (deserializeFromContent)
                    {
                        var textNode = root.Nodes().FirstOrDefault(n => n is XText);
                        if (textNode != null)
                        {
                            value = ((XText)textNode).Value;
                            prop.SetValue(x, value, null);
                        }
                        continue;
                    }

                    // special case for inline list items
                    if (type.IsGenericType)
                    {
                        var genericType = type.GetGenericArguments()[0];
                        var first       = GetElementByName(root, genericType.Name);
                        var list        = (IList)Activator.CreateInstance(type.AsType());

                        if (first != null && root != null)
                        {
                            var elements = root.Elements(first.Name);

                            PopulateListFromElements(genericType, elements, list);
                        }

                        prop.SetValue(x, list, null);
                    }
                    continue;
                }

                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    // if the value is empty, set the property to null...
                    if (string.IsNullOrEmpty(value.ToString()))
                    {
                        prop.SetValue(x, null, null);
                        continue;
                    }

                    type = type.GetGenericArguments()[0].GetTypeInfo();
                }

                var asType = type.AsType();
                if (asType == typeof(bool))
                {
                    var toConvert = value.ToString()
                                    .ToLower();

                    prop.SetValue(x, XmlConvert.ToBoolean(toConvert), null);
                }
                else if (type.IsPrimitive)
                {
                    prop.SetValue(x, value.ChangeType(asType, Culture), null);
                }
                else if (type.IsEnum)
                {
                    var converted = type.AsType().FindEnumValue(value.ToString(), Culture);

                    prop.SetValue(x, converted, null);
                }
                else if (asType == typeof(Uri))
                {
                    var uri = new Uri(value.ToString(), UriKind.RelativeOrAbsolute);

                    prop.SetValue(x, uri, null);
                }
                else if (asType == typeof(string))
                {
                    prop.SetValue(x, value, null);
                }
                else if (asType == typeof(DateTime))
                {
                    value = DateFormat.HasValue()
                        ? DateTime.ParseExact(value.ToString(), DateFormat, Culture)
                        : DateTime.Parse(value.ToString(), Culture);

                    prop.SetValue(x, value, null);
                }
                else if (asType == typeof(DateTimeOffset))
                {
                    var toConvert = value.ToString();

                    if (string.IsNullOrEmpty(toConvert))
                    {
                        continue;
                    }

                    DateTimeOffset deserialisedValue;

                    try
                    {
                        deserialisedValue = XmlConvert.ToDateTimeOffset(toConvert);
                        prop.SetValue(x, deserialisedValue, null);
                    }
                    catch (Exception)
                    {
                        if (TryGetFromString(toConvert, out var result, asType))
                        {
                            prop.SetValue(x, result, null);
                        }
                        else
                        {
                            //fallback to parse
                            deserialisedValue = DateTimeOffset.Parse(toConvert);
                            prop.SetValue(x, deserialisedValue, null);
                        }
                    }
                }
        private object HandleListDerivative(XElement root, string propName, Type type)
        {
#if !WINDOWS_UWP
            Type t = type.IsGenericType
             ? type.GetGenericArguments()[0]
             : type.BaseType.GetGenericArguments()[0];
#else
            Type t = type.GetTypeInfo().IsGenericType
               ? type.GetGenericArguments()[0]
               : type.GetTypeInfo().BaseType.GetGenericArguments()[0];
#endif
            IList list = ( IList )Activator.CreateInstance(type);

            IList <XElement> elements = root.Descendants(t.Name.AsNamespaced(this.Namespace)).ToList();

            if (!elements.Any())
            {
                elements = root.Descendants("struct").ToList();
            }

            string name = t.Name;
            DeserializeAsAttribute attribute = t.GetAttribute <DeserializeAsAttribute>();

            if (attribute != null)
            {
                name = attribute.Name;
            }

            if (!elements.Any())
            {
                XName lowerName = name.ToLower().AsNamespaced(this.Namespace);

                elements = root.Descendants(lowerName).ToList();
            }

            if (!elements.Any())
            {
                XName camelName = name.ToCamelCase(this.Culture).AsNamespaced(this.Namespace);

                elements = root.Descendants(camelName).ToList();
            }

            if (!elements.Any())
            {
                elements = root.Descendants()
                           .Where(e => e.Name.LocalName.RemoveUnderscoresAndDashes() == name)
                           .ToList();
            }

            if (!elements.Any())
            {
                var props = t.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
                            .Where(p => p.GetCustomAttribute <SerializeAsAttribute>() != null)
                            .OrderBy(p => p.GetCustomAttribute <SerializeAsAttribute>().Index)
                            .ToArray();
                if (props.Any())
                {
                    elements = root.Elements().ToList();
                    elements.Each((element, i) => {
                        element.Elements().Each((child, j) => child.Name = props[j].Name);
                    });
                }
            }

            if (!elements.Any())
            {
                XName lowerName = name.ToLower().AsNamespaced(this.Namespace);

                elements = root.Descendants()
                           .Where(e => e.Name.LocalName.RemoveUnderscoresAndDashes() == lowerName)
                           .ToList();
            }

            if (!elements.Any())
            {
                elements = root.Descendants()
                           .ToList();
            }

            this.PopulateListFromElements(t, elements, list);

            // get properties too, not just list items
            // only if this isn't a generic type
#if !WINDOWS_UWP
            if (!type.IsGenericType)
#else
            if (!type.GetTypeInfo().IsGenericType)
#endif
            {
                this.Map(list, root.Element(propName.AsNamespaced(this.Namespace)) ?? root);
                // when using RootElement, the heirarchy is different
            }

            return(list);
        }
        protected virtual object Map(object x, XElement root)
        {
            Type objType = x.GetType();

            PropertyInfo[] props = objType.GetProperties();

            foreach (PropertyInfo prop in props)
            {
                Type type = prop.PropertyType;

#if !WINDOWS_UWP
                bool typeIsPublic = type.IsPublic || type.IsNestedPublic;
#else
                bool typeIsPublic = type.GetTypeInfo().IsPublic || type.GetTypeInfo().IsNestedPublic;
#endif

                if (!typeIsPublic || !prop.CanWrite)
                {
                    continue;
                }

                XName name;
#if !WINDOWS_UWP
                object[] attributes = prop.GetCustomAttributes(typeof(DeserializeAsAttribute), false);

                if (attributes.Length > 0)
#else
                IEnumerable <Attribute> attributes = prop.GetCustomAttributes(typeof(DeserializeAsAttribute), false);

                if (attributes.Count() > 0)
#endif
                {
                    DeserializeAsAttribute attribute = ( DeserializeAsAttribute )attributes.First();

                    name = attribute.Name.AsNamespaced(this.Namespace);
                }
                else
                {
                    name = prop.Name.AsNamespaced(this.Namespace);
                }

                object value = this.GetValueFromXml(root, name, prop);

                if (value == null)
                {
                    // special case for inline list items
#if !WINDOWS_UWP
                    if (type.IsGenericType)
#else
                    if (type.GetTypeInfo().IsGenericType)
#endif
                    {
                        Type     genericType = type.GetGenericArguments()[0];
                        XElement first       = this.GetElementByName(root, genericType.Name);
                        IList    list        = ( IList )Activator.CreateInstance(type);

                        if (first != null && root != null)
                        {
                            IEnumerable <XElement> elements = root.Elements(first.Name);

                            this.PopulateListFromElements(genericType, elements, list);
                        }

                        prop.SetValue(x, list, null);
                    }
                    continue;
                }

                // check for nullable and extract underlying type
#if !WINDOWS_UWP
                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
#else
                if (type.GetTypeInfo().IsGenericType&& type.GetGenericTypeDefinition() == typeof(Nullable <>))
#endif
                {
                    // if the value is empty, set the property to null...
                    if (string.IsNullOrEmpty(value.ToString()))
                    {
                        prop.SetValue(x, null, null);
                        continue;
                    }

                    type = type.GetGenericArguments()[0];
                }

                if (type == typeof(bool))
                {
                    string toConvert = value.ToString()
                                       .ToLower();

                    prop.SetValue(x, XmlConvert.ToBoolean(toConvert), null);
                }
#if !WINDOWS_UWP
                else if (type.IsPrimitive)
#else
                else if (type.GetTypeInfo().IsPrimitive)
#endif
                {
                    prop.SetValue(x, value.ChangeType(type, this.Culture), null);
                }
#if !WINDOWS_UWP
                else if (type.IsEnum)
#else
                else if (type.GetTypeInfo().IsEnum)
#endif
                {
                    object converted = type.FindEnumValue(value.ToString(), this.Culture);

                    prop.SetValue(x, converted, null);
                }
                else if (type == typeof(Uri))
                {
                    Uri uri = new Uri(value.ToString(), UriKind.RelativeOrAbsolute);

                    prop.SetValue(x, uri, null);
                }
                else if (type == typeof(string))
                {
                    prop.SetValue(x, value, null);
                }
                else if (type == typeof(DateTime))
                {
                    value = this.DateFormat.HasValue()
                   ? DateTime.ParseExact(value.ToString(), this.DateFormat, this.Culture)
                   : DateTime.Parse(value.ToString(), this.Culture);

                    prop.SetValue(x, value, null);
                }
                else if (type == typeof(DateTimeOffset))
                {
                    string toConvert = value.ToString();

                    if (!string.IsNullOrEmpty(toConvert))
                    {
                        DateTimeOffset deserialisedValue;

                        try {
                            deserialisedValue = XmlConvert.ToDateTimeOffset(toConvert);
                            prop.SetValue(x, deserialisedValue, null);
                        } catch (Exception) {
                            object result;

                            if (TryGetFromString(toConvert, out result, type))
                            {
                                prop.SetValue(x, result, null);
                            }
                            else
                            {
                                //fallback to parse
                                deserialisedValue = DateTimeOffset.Parse(toConvert);
                                prop.SetValue(x, deserialisedValue, null);
                            }
                        }
                    }
                }
                else if (type == typeof(decimal))
                {
                    value = decimal.Parse(value.ToString(), this.Culture);
                    prop.SetValue(x, value, null);
                }
                else if (type == typeof(Guid))
                {
                    string raw = value.ToString();

                    value = string.IsNullOrEmpty(raw)
                   ? Guid.Empty
                   : new Guid(value.ToString());

                    prop.SetValue(x, value, null);
                }
                else if (type == typeof(TimeSpan))
                {
                    TimeSpan timeSpan = XmlConvert.ToTimeSpan(value.ToString());

                    prop.SetValue(x, timeSpan, null);
                }
#if !WINDOWS_UWP
                else if (type.IsGenericType)
#else
                else if (type.GetTypeInfo().IsGenericType)
#endif
                {
                    Type     t         = type.GetGenericArguments()[0];
                    IList    list      = ( IList )Activator.CreateInstance(type);
                    XElement container = this.GetElementByName(root, prop.Name.AsNamespaced(this.Namespace));

                    if (container.HasElements)
                    {
                        XElement first = container.Elements().FirstOrDefault();

                        if (first != null)
                        {
                            IEnumerable <XElement> elements = container.Elements(first.Name);

                            this.PopulateListFromElements(t, elements, list);
                        }
                    }

                    prop.SetValue(x, list, null);
                }
                else if (type.IsSubclassOfRawGeneric(typeof(List <>)))
                {
                    // handles classes that derive from List<T>
                    // e.g. a collection that also has attributes
                    object list = this.HandleListDerivative(root, prop.Name, type);

                    prop.SetValue(x, list, null);
                }
                else
                {
                    //fallback to type converters if possible
                    object result;

                    if (TryGetFromString(value.ToString(), out result, type))
                    {
                        prop.SetValue(x, result, null);
                    }
                    else
                    {
                        // nested property classes
                        if (root != null)
                        {
                            XElement element = this.GetElementByName(root, name);

                            if (element != null)
                            {
                                object item = this.CreateAndMap(type, element);

                                prop.SetValue(x, item, null);
                            }
                        }
                    }
                }
            }

            return(x);
        }
Ejemplo n.º 9
0
        protected virtual object Map(object x, XElement root)
        {
            Type type = x.GetType();

            PropertyInfo[] properties = type.GetProperties();
            PropertyInfo[] array      = properties;
            foreach (PropertyInfo propertyInfo in array)
            {
                Type type2 = propertyInfo.PropertyType;
                if ((type2.IsPublic || type2.IsNestedPublic) && propertyInfo.CanWrite)
                {
                    object[] customAttributes = propertyInfo.GetCustomAttributes(typeof(DeserializeAsAttribute), false);
                    XName    name;
                    if (customAttributes.Length > 0)
                    {
                        DeserializeAsAttribute deserializeAsAttribute = (DeserializeAsAttribute)customAttributes[0];
                        name = deserializeAsAttribute.Name.AsNamespaced(Namespace);
                    }
                    else
                    {
                        name = propertyInfo.Name.AsNamespaced(Namespace);
                    }
                    object valueFromXml = GetValueFromXml(root, name, propertyInfo);
                    if (valueFromXml == null)
                    {
                        if (type2.IsGenericType)
                        {
                            Type     type3         = type2.GetGenericArguments()[0];
                            XElement elementByName = GetElementByName(root, type3.Name);
                            IList    list          = (IList)Activator.CreateInstance(type2);
                            if (elementByName != null)
                            {
                                IEnumerable <XElement> elements = root.Elements(elementByName.Name);
                                PopulateListFromElements(type3, elements, list);
                            }
                            propertyInfo.SetValue(x, list, null);
                        }
                    }
                    else
                    {
                        if (type2.IsGenericType && type2.GetGenericTypeDefinition() == typeof(Nullable <>))
                        {
                            if (valueFromXml == null || string.IsNullOrEmpty(valueFromXml.ToString()))
                            {
                                propertyInfo.SetValue(x, null, null);
                                continue;
                            }
                            type2 = type2.GetGenericArguments()[0];
                        }
                        object result;
                        if (type2 == typeof(bool))
                        {
                            string s = valueFromXml.ToString().ToLower();
                            propertyInfo.SetValue(x, XmlConvert.ToBoolean(s), null);
                        }
                        else if (type2.IsPrimitive)
                        {
                            propertyInfo.SetValue(x, valueFromXml.ChangeType(type2, Culture), null);
                        }
                        else if (type2.IsEnum)
                        {
                            object value = type2.FindEnumValue(valueFromXml.ToString(), Culture);
                            propertyInfo.SetValue(x, value, null);
                        }
                        else if (type2 == typeof(Uri))
                        {
                            Uri value2 = new Uri(valueFromXml.ToString(), UriKind.RelativeOrAbsolute);
                            propertyInfo.SetValue(x, value2, null);
                        }
                        else if (type2 == typeof(string))
                        {
                            propertyInfo.SetValue(x, valueFromXml, null);
                        }
                        else if (type2 == typeof(DateTime))
                        {
                            valueFromXml = ((!DateFormat.HasValue()) ? ((object)DateTime.Parse(valueFromXml.ToString(), Culture)) : ((object)DateTime.ParseExact(valueFromXml.ToString(), DateFormat, Culture)));
                            propertyInfo.SetValue(x, valueFromXml, null);
                        }
                        else if (type2 == typeof(DateTimeOffset))
                        {
                            string s = valueFromXml.ToString();
                            if (!string.IsNullOrEmpty(s))
                            {
                                try
                                {
                                    DateTimeOffset dateTimeOffset = XmlConvert.ToDateTimeOffset(s);
                                    propertyInfo.SetValue(x, dateTimeOffset, null);
                                }
                                catch (Exception)
                                {
                                    if (TryGetFromString(s, out result, type2))
                                    {
                                        propertyInfo.SetValue(x, result, null);
                                    }
                                    else
                                    {
                                        DateTimeOffset dateTimeOffset = DateTimeOffset.Parse(s);
                                        propertyInfo.SetValue(x, dateTimeOffset, null);
                                    }
                                }
                            }
                        }
                        else if (type2 == typeof(decimal))
                        {
                            valueFromXml = decimal.Parse(valueFromXml.ToString(), Culture);
                            propertyInfo.SetValue(x, valueFromXml, null);
                        }
                        else if (type2 == typeof(Guid))
                        {
                            string value3 = valueFromXml.ToString();
                            valueFromXml = (string.IsNullOrEmpty(value3) ? Guid.Empty : new Guid(valueFromXml.ToString()));
                            propertyInfo.SetValue(x, valueFromXml, null);
                        }
                        else if (type2 == typeof(TimeSpan))
                        {
                            TimeSpan timeSpan = XmlConvert.ToTimeSpan(valueFromXml.ToString());
                            propertyInfo.SetValue(x, timeSpan, null);
                        }
                        else if (type2.IsGenericType)
                        {
                            Type     t              = type2.GetGenericArguments()[0];
                            IList    list           = (IList)Activator.CreateInstance(type2);
                            XElement elementByName2 = GetElementByName(root, propertyInfo.Name.AsNamespaced(Namespace));
                            if (elementByName2.HasElements)
                            {
                                XElement elementByName          = elementByName2.Elements().FirstOrDefault();
                                IEnumerable <XElement> elements = elementByName2.Elements(elementByName.Name);
                                PopulateListFromElements(t, elements, list);
                            }
                            propertyInfo.SetValue(x, list, null);
                        }
                        else if (type2.IsSubclassOfRawGeneric(typeof(List <>)))
                        {
                            object value4 = HandleListDerivative(x, root, propertyInfo.Name, type2);
                            propertyInfo.SetValue(x, value4, null);
                        }
                        else if (TryGetFromString(valueFromXml.ToString(), out result, type2))
                        {
                            propertyInfo.SetValue(x, result, null);
                        }
                        else if (root != null)
                        {
                            XElement elementByName3 = GetElementByName(root, name);
                            if (elementByName3 != null)
                            {
                                object value5 = CreateAndMap(type2, elementByName3);
                                propertyInfo.SetValue(x, value5, null);
                            }
                        }
                    }
                }
            }
            return(x);
        }