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