view Implab.ServiceHost/Unity/SerializedElement.cs @ 273:79110a16cab7 v3

Working on Unity xml configuration: Refactoring in progress
author cin
date Thu, 26 Apr 2018 19:35:01 +0300
parents
children 22629bf26121
line wrap: on
line source

using System;
using System.Xml;
using System.Xml.Serialization;

namespace Implab.ServiceHost.Unity
{
    public class SerializedElement : AbstractRegistration {
        [XmlAttribute("href")]
        public string Location { get; set; }

        [XmlAttribute("serializedType")]
        public string SerializedType { get; set; }


        [XmlAnyElement]
        public XmlElement[] Content { get; set; }
        
        public override void Visit(ContainerContext context) {
            context.Visit(this);
        }

        public virtual Type ResolveSerializedType(ContainerContext context) {
            if(string.IsNullOrEmpty(SerializedType))
                return ResolveRegistrationType(context);
            return context.Resolve(SerializedType);
        }

        public virtual object GetValue(ContainerContext context) {
            var type = ResolveRegistrationType(context);
            if (Content == null || Content.Length == 0)
                return Safe.CreateDefaultValue(type);

            var serializer = new XmlSerializer(type);
            using(var reader = Content[0].CreateNavigator().ReadSubtree())
                return serializer.Deserialize(reader);
        }
    }
}