Пример #1
0
        public void Deserialize(Type type, XElement element, object target)
        {
            if (element == null || target == null)
            {
                return;
            }
            if (type.IsGenericType)
            {
                Type genericTypeDefinition = type.GetGenericTypeDefinition();
                Type entryType             = type.GetGenericArguments()[0];
                if (typeof(IList).IsAssignableFrom(genericTypeDefinition))
                {
                    IList list = (IList)target;
                    foreach (XElement entryElement in element.Elements())
                    {
                        object entry = this.Deserialize(entryType, entryElement);
                        list.Add(entry);
                    }
                    return;
                }
            }
            IEnumerable <XElement> propertieElements = element.Elements();

            foreach (XElement propertyElement in propertieElements)
            {
                XmlMapping   entry        = this.mapping.Get(propertyElement);
                string       propertyName = entry?.TargetName ?? propertyElement.Name.LocalName;
                PropertyInfo propertyInfo = type.GetProperty(propertyName, BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.Public);
                if (propertyInfo == null)
                {
                    continue;
                }
                object value = propertyInfo.CanRead ? propertyInfo.GetGetMethod()?.Invoke(target, null) : null;
                if (entry != null && entry.TargetIsList)
                {
                    if (value == null)
                    {
                        this.CheckCanWriteProperty(propertyInfo);
                        value = this.Create(propertyInfo.PropertyType, propertyElement);
                        propertyInfo.GetSetMethod(true).Invoke(target, new[] { value });
                    }
                    Type  entryType = entry.TargetType ?? propertyInfo.PropertyType.GetGenericArguments()[0];
                    IList list      = (IList)value;
                    list.Add(this.Deserialize(entryType, propertyElement));
                }
                else if (entry?.Constructor != null)
                {
                    this.CheckCanWriteProperty(propertyInfo);
                    value = entry.Constructor.Invoke(propertyElement);
                    propertyInfo.GetSetMethod(true).Invoke(target, new[] { value });
                }
                else
                {
                    Type valueType = entry?.TargetType ?? propertyInfo.PropertyType;
                    if (value == null || valueType.IsValueType)
                    {
                        this.CheckCanWriteProperty(propertyInfo);
                        value = this.Deserialize(valueType, propertyElement);
                        propertyInfo.GetSetMethod(true).Invoke(target, new[] { value });
                    }
                    else
                    {
                        this.Deserialize(valueType, propertyElement, value);
                    }
                }
            }
        }
Пример #2
0
 public IXmlMappingAfterCreate Path(string path)
 {
     this.mapping = XmlMapping.FromPath(path);
     this.mappingList.Add(this.mapping);
     return(this);
 }
Пример #3
0
 public IXmlMappingAfterCreate Type(Type type)
 {
     this.mapping = XmlMapping.FromType(type);
     this.mappingList.Add(this.mapping);
     return(this);
 }
Пример #4
0
 public IXmlMappingAfterCreate Name(string name)
 {
     this.mapping = XmlMapping.FromName(name);
     this.mappingList.Add(this.mapping);
     return(this);
 }