Esempio n. 1
0
        private static void DeSerialize(this IXSettings obj, XElement xElement)
        {
            foreach (var propertyInfo in obj.GetType().GetProperties())
            {
                if (propertyInfo.GetCustomAttribute(typeof(XmlIgnoreAttribute)) == null && propertyInfo.CanWrite && propertyInfo.CanRead)
                {
                    try
                    {
                        string elementName            = propertyInfo.Name;
                        XmlElementAttribute attribute = (XmlElementAttribute)propertyInfo.GetCustomAttribute(typeof(XmlElementAttribute));
                        if (attribute != null)
                        {
                            elementName = attribute.ElementName;
                        }

                        XElement element     = xElement.Element(elementName);
                        Type     contentType = MyTypeExtension.GetAssemblyQualifiedType(element.Attribute("type").Value);



                        object o = ToObject(element, contentType);
                        propertyInfo.SetValue(obj, o);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 将<see cref="XElement"/>转化为等价<see cref="object"/>.
        /// </summary>
        /// <param name="element"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        internal static dynamic ToObject(XElement element, Type type)
        {
            ConvertType t = GetConvertType(type);

            if (t == ConvertType.Convert)
            {
                return(Convert.ChangeType(element.Value, type));
            }
            else if (t == ConvertType.Transfer)
            {
                string s = element.Value;
                object o = null;
                if (type == typeof(System.Windows.Size))
                {
                    o = System.Windows.Size.Parse(s);
                }
                else if (type == typeof(System.Windows.Point))
                {
                    o = System.Windows.Point.Parse(s);
                }
                else if (type == typeof(System.Windows.Media.Color))
                {
                    string[] x = s.Split(',');
                    o = System.Windows.Media.Color.FromArgb(byte.Parse(x[0]), byte.Parse(x[1]), byte.Parse(x[2]), byte.Parse(x[3]));
                }
                else if (type == typeof(DateTime))
                {
                    o = DateTime.Parse(s);
                }
                return(o);
            }
            else if (t == ConvertType.Enum)
            {
                string s = element.Value;
                return(Enum.Parse(type, s));
            }
            else if (t == ConvertType.Collection)
            {
                dynamic result = null;
                if (type.IsArray)
                {
                    Type membertype = type.GetElementType();
                    Type listtype   = typeof(List <>);
                    listtype = listtype.MakeGenericType(membertype);
                    result   = Activator.CreateInstance(listtype);
                    foreach (var item in element.Elements())
                    {
                        result.Add(ToObject(item, membertype));
                    }
                    return(result.ToArray());
                }
                else if (type.GetGenericTypeDefinition() == typeof(List <>))
                {
                    Type membertype = type.GenericTypeArguments[0];
                    result = Activator.CreateInstance(type);
                    foreach (var item in element.Elements())
                    {
                        result.Add(ToObject(item, membertype));
                    }
                    return(result);
                }
                else
                {
                    return(null);
                }
            }
            else if (t == ConvertType.Dictionary)
            {
                Type    keyType          = type.GenericTypeArguments[0];
                Type    valueType        = type.GenericTypeArguments[1];
                Type    keyValuePairType = (typeof(KeyValuePair <,>)).MakeGenericType(keyType, valueType);
                dynamic dic = Activator.CreateInstance(type);
                foreach (var item in element.Elements())
                {
                    dynamic key   = ToObject(item.Element("Key"), keyType);
                    dynamic value = ToObject(item.Element("Value"), valueType);
                    dic.Add(key, value);
                }

                return(dic);
            }
            else
            {
                object result = Activator.CreateInstance(type);

                foreach (var propertyInfo in type.GetProperties())
                {
                    string elementName            = propertyInfo.Name;
                    XmlElementAttribute attribute = (XmlElementAttribute)propertyInfo.GetCustomAttribute(typeof(XmlElementAttribute));
                    if (attribute != null)
                    {
                        elementName = attribute.ElementName;
                    }

                    XElement e           = element.Element(elementName);
                    Type     contentType = MyTypeExtension.GetAssemblyQualifiedType(e.Attribute("type").Value);

                    object o = ToObject(e, contentType);
                    propertyInfo.SetValue(result, o);
                }

                return(result);
            }
        }