Exemplo n.º 1
0
 /// <summary>
 /// Creates the related business object from XML element.
 /// </summary>
 /// <param name="element">Xml element from which to create the <see cref="IBusinessObject"/>.</param>
 /// <param name="relatedObjectType">Type of the related object.</param>
 /// <returns>Related object.</returns>
 public static IBusinessObject CreateRelatedBusinessObjectFromXmlElement(XElement element, string relatedObjectType)
 {
     return(BusinessObjectHelper.CreateRelatedBusinessObjectFromXmlElement(element, (BusinessObjectType)Enum.Parse(typeof(BusinessObjectType), relatedObjectType, true)));
 }
Exemplo n.º 2
0
        public static void DeserializeSingleValue(object target, PropertyInfo propertyInfo, XmlSerializableAttribute attribute, XElement boElement)
        {
            Type type = propertyInfo.PropertyType;

            if (!String.IsNullOrEmpty(attribute.EncapsulatingXmlField))
            {
                boElement = boElement.Element(attribute.EncapsulatingXmlField);
            }

            /*if (boElement == null ||
             *  (!attribute.UseAttribute && attribute.XmlField != null && boElement.Element(attribute.XmlField) == null) ||
             *  (attribute.UseAttribute && attribute.XmlField != null && boElement.Attribute(attribute.XmlField) == null))
             *  propertyInfo.SetValue(target, null, null);
             * else*/
            if (boElement != null && attribute.XmlField != null)
            {
                if (boElement.Element(attribute.XmlField) != null || boElement.Attribute(attribute.XmlField) != null)
                {
                    if (propertyInfo.PropertyType == typeof(Boolean))
                    {
                        if (BusinessObjectHelper.ReadValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute) == "1")
                        {
                            propertyInfo.SetValue(target, true, null);
                        }
                        else
                        {
                            propertyInfo.SetValue(target, false, null);
                        }
                    }
                    else if (type == typeof(String))
                    {
                        propertyInfo.SetValue(target, BusinessObjectHelper.ReadValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute), null);
                    }
                    else if (type == typeof(Decimal) || type == typeof(Decimal?))
                    {
                        propertyInfo.SetValue(target, Convert.ToDecimal(BusinessObjectHelper.ReadValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute), CultureInfo.InvariantCulture), null);
                    }
                    else if (type == typeof(Int32) || type == typeof(Int32?))
                    {
                        propertyInfo.SetValue(target, Convert.ToInt32(BusinessObjectHelper.ReadValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute), CultureInfo.InvariantCulture), null);
                    }
                    else if (type == typeof(Guid) || type == typeof(Guid?))
                    {
                        propertyInfo.SetValue(target, new Guid(BusinessObjectHelper.ReadValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute)), null);
                    }
                    else if (type == typeof(DateTime) || type == typeof(DateTime?))
                    {
                        propertyInfo.SetValue(target, DateTime.Parse(BusinessObjectHelper.ReadValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute), CultureInfo.InvariantCulture), null);
                    }
                    else if (type == typeof(XElement))
                    {
                        XElement element = BusinessObjectHelper.ReadXmlValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute);

                        if (element.FirstNode is XElement && !attribute.UseAttribute)
                        {
                            propertyInfo.SetValue(target, new XElement((XElement)element.FirstNode), null);
                        }
                        else if (!attribute.UseAttribute)
                        {
                            propertyInfo.SetValue(target, new XElement(attribute.XmlField, element.Value), null);
                        }
                        else
                        {
                            propertyInfo.SetValue(target, element, null);
                        }
                    }
                    else if (typeof(IBusinessObject).IsAssignableFrom(type))
                    {
                        if (!String.IsNullOrEmpty(attribute.XmlField))
                        {
                            IBusinessObject obj = BusinessObjectHelper.CreateRelatedBusinessObjectFromXmlElement((XElement)boElement.Element(attribute.XmlField).FirstNode, attribute.RelatedObjectType);
                            propertyInfo.SetValue(target, obj, null);
                        }
                        else                         //check if the nested object has attribute indicating from which node to deserialize him
                        {
                            if (BusinessObject.ClassXmlSerializationCache.ContainsKey(type))
                            {
                                XmlSerializationCache cache = BusinessObject.ClassXmlSerializationCache[type];

                                IBusinessObject obj = (IBusinessObject)Activator.CreateInstance(type, target);                                 //constructor(parent)
                                obj.Deserialize((XElement)boElement.Element(cache.Attribute.XmlField));
                                propertyInfo.SetValue(target, obj, null);
                            }
                        }
                    }
                    else if (type.IsEnum)
                    {
                        propertyInfo.SetValue(target, Convert.ToInt32(BusinessObjectHelper.ReadValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute), CultureInfo.InvariantCulture), null);
                    }
                    else if (typeof(ISerializableBusinessObjectContainer).IsAssignableFrom(type))
                    {
                        ((ISerializableBusinessObjectContainer)propertyInfo.GetValue(target, null)).Deserialize(boElement.Element(attribute.XmlField));
                    }
                    else
                    {
                        throw new InvalidOperationException("Unknown type to deserialize");
                    }
                }
            }
            else if (attribute.OverrideWithEmptyValue && type == typeof(Nullable))
            {
                ///DO przemyślenia
                //propertyInfo.SetValue(target, null, null);
            }
        }