comparison 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
comparison
equal deleted inserted replaced
272:9d1cca834b05 273:79110a16cab7
1 using System;
2 using System.Xml;
3 using System.Xml.Serialization;
4
5 namespace Implab.ServiceHost.Unity
6 {
7 public class SerializedElement : AbstractRegistration {
8 [XmlAttribute("href")]
9 public string Location { get; set; }
10
11 [XmlAttribute("serializedType")]
12 public string SerializedType { get; set; }
13
14
15 [XmlAnyElement]
16 public XmlElement[] Content { get; set; }
17
18 public override void Visit(ContainerContext context) {
19 context.Visit(this);
20 }
21
22 public virtual Type ResolveSerializedType(ContainerContext context) {
23 if(string.IsNullOrEmpty(SerializedType))
24 return ResolveRegistrationType(context);
25 return context.Resolve(SerializedType);
26 }
27
28 public virtual object GetValue(ContainerContext context) {
29 var type = ResolveRegistrationType(context);
30 if (Content == null || Content.Length == 0)
31 return Safe.CreateDefaultValue(type);
32
33 var serializer = new XmlSerializer(type);
34 using(var reader = Content[0].CreateNavigator().ReadSubtree())
35 return serializer.Deserialize(reader);
36 }
37 }
38 }