comparison Implab.ServiceHost/Unity/SerializedElement.cs @ 274:22629bf26121 v3

Unity xml configuration, alpha2
author cin
date Fri, 27 Apr 2018 04:47:52 +0300
parents 79110a16cab7
children 6fefd5811b9b
comparison
equal deleted inserted replaced
273:79110a16cab7 274:22629bf26121
2 using System.Xml; 2 using System.Xml;
3 using System.Xml.Serialization; 3 using System.Xml.Serialization;
4 4
5 namespace Implab.ServiceHost.Unity 5 namespace Implab.ServiceHost.Unity
6 { 6 {
7 public class SerializedElement : AbstractRegistration { 7 public class SerializedElement : AbstractRegistration, ISerializedValue {
8 [XmlAttribute("href")] 8 [XmlAttribute("href")]
9 public string Location { get; set; } 9 public string Location { get; set; }
10 10
11 [XmlAttribute("serializedType")] 11 [XmlAttribute("serializedType")]
12 public string SerializedType { get; set; } 12 public string SerializedType { get; set; }
13 13
14 14
15 [XmlAnyElement] 15 [XmlAnyElement]
16 public XmlElement[] Content { get; set; } 16 public XmlElement[] Content { get; set; }
17 17
18 public override void Visit(ContainerContext context) { 18 string ISerializedValue.TypeName {
19 get {
20 return string.IsNullOrEmpty(SerializedType) ? RegistrationType : SerializedType;
21 }
22 }
23
24 public override void Visit(ContainerBuilder context) {
19 context.Visit(this); 25 context.Visit(this);
20 } 26 }
21 27
22 public virtual Type ResolveSerializedType(ContainerContext context) { 28 public XmlReader GetReader() {
23 if(string.IsNullOrEmpty(SerializedType)) 29 if (!string.IsNullOrEmpty(Location))
24 return ResolveRegistrationType(context); 30 return XmlReader.Create(Location);
25 return context.Resolve(SerializedType); 31 if (Content != null && Content.Length > 0)
26 } 32 return Content[0].CreateNavigator().ReadSubtree();
27 33
28 public virtual object GetValue(ContainerContext context) { 34 throw new Exception("No content found, expected XML document");
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 } 35 }
37 } 36 }
38 } 37 }