Esempio n. 1
0
        XElement SerializeProperty(PropertyDescriptor property)
        {
            var document   = new XDocument();
            var serializer = PropertySerializer.GetXmlSerializer(property.Name, property.PropertyType);

            using (var writer = document.CreateWriter())
            {
                serializer.Serialize(writer, property.GetValue(this), DefaultSerializerNamespaces);
            }
            return(document.Root);
        }
Esempio n. 2
0
        void DeserializeProperty(XElement element, PropertyDescriptor property)
        {
            if (property.PropertyType == typeof(XElement))
            {
                property.SetValue(this, element);
                return;
            }

            var serializer = PropertySerializer.GetXmlSerializer(property.Name, property.PropertyType);

            using (var reader = element.CreateReader())
            {
                var value = serializer.Deserialize(reader);
                if (property.IsReadOnly)
                {
                    var collection = (IList)property.GetValue(this);
                    if (collection == null)
                    {
                        throw new InvalidOperationException("Collection reference not set to an instance of an object.");
                    }

                    collection.Clear();
                    var collectionElements = value as IEnumerable;
                    if (collectionElements != null)
                    {
                        foreach (var collectionElement in collectionElements)
                        {
                            collection.Add(collectionElement);
                        }
                    }
                }
                else
                {
                    property.SetValue(this, value);
                }
            }
        }