annotate Implab.ServiceHost/Unity/InjectionValueBuilder.cs @ 274:22629bf26121 v3

Unity xml configuration, alpha2
author cin
date Fri, 27 Apr 2018 04:47:52 +0300
parents
children 963b17c275be
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
274
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
1 using System;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
2 using System.ComponentModel;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
3 using System.Xml.Serialization;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
4 using Unity.Injection;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
5
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
6 namespace Implab.ServiceHost.Unity {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
7
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
8 public class InjectionValueBuilder {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
9
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
10 readonly TypeResolver m_resolver;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
11
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
12 public Type DefaultType { get; private set; }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
13
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
14 public Type ValueType { get; set; }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
15
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
16 public object Value { get; set; }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
17
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
18 public InjectionParameterValue Injection {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
19 get {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
20 if (Value != null)
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
21 return InjectionParameterValue.ToParameter(Value);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
22
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
23 return new InjectionParameter(ValueType, null);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
24 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
25 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
26
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
27 internal InjectionValueBuilder(TypeResolver resolver, Type acceptsType) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
28 m_resolver = resolver;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
29 DefaultType = acceptsType;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
30 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
31
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
32 public Type ResolveType(string typeSpec) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
33 if (string.IsNullOrEmpty(typeSpec)) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
34 if (DefaultType == null)
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
35 throw new Exception("The type must be specified");
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
36 return DefaultType;
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
37 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
38 return m_resolver.Resolve(typeSpec);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
39 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
40
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
41 public void Visit(ITextValue value) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
42 ValueType = ResolveType(value.TypeName);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
43
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
44 Value = string.IsNullOrEmpty(value.Value) ?
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
45 Safe.CreateDefaultValue(ValueType) :
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
46 TypeDescriptor.GetConverter(ValueType).ConvertFromString(value.Value);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
47 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
48
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
49 public void Visit(ISerializedValue value) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
50 ValueType = ResolveType(value.TypeName);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
51
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
52 var serializer = new XmlSerializer(ValueType);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
53
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
54 using (var reader = value.GetReader())
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
55 Value = new InjectionParameter(ValueType, serializer.Deserialize(reader));
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
56 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
57
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
58 public void Visit(IDependencyReference value) {
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
59 ValueType = ResolveType(value.TypeName);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
60 Value = new ResolvedParameter(ValueType, value.DependencyName);
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
61 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
62
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
63 }
22629bf26121 Unity xml configuration, alpha2
cin
parents:
diff changeset
64 }