Ejemplo n.º 1
0
        public void Read(IValueProvider value, XElement serialized)
        {
            string name     = serialized.Name.LocalName;
            var    type     = FindDerivedType(value.Type, name);
            var    instance = Activator.CreateInstance(type);

            var discoveryStrategy = ValueDiscoveryStrategy.Get(type) ?? valueDiscovery;
            var values            = discoveryStrategy.GetValues(instance);

            foreach (var property in values)
            {
                XAttribute attribute = serialized.Attribute(property.Name);
                if (attribute != null)
                {
                    SerializationService.Instance.Read(property, attribute.Value);
                    continue;
                }

                XElement subElement = serialized.Element(property.Name);
                if (subElement != null)
                {
                    SerializationService.Instance.Read(property, subElement);
                    continue;
                }
            }

            value.SetValue(instance);
        }
Ejemplo n.º 2
0
        public object Write(IValueProvider value)
        {
            object   instance = value.GetValue <object>();
            XElement result   = new XElement(value.Name);

            var discoveryStrategy = ValueDiscoveryStrategy.Get(instance.GetType()) ?? valueDiscovery;
            var values            = discoveryStrategy.GetValues(instance);

            foreach (var property in values)
            {
                object serialized = SerializationService.Instance.Write(property);

                if (serialized is string)
                {
                    XAttribute attribute = new XAttribute(property.Name, serialized.ToString());
                    result.Add(attribute);
                }
                else if (serialized is XElement)
                {
                    result.Add(new XElement(property.Name, serialized));
                }
            }

            return(result);
        }