Mercurial > pub > ImplabNet
annotate Implab.ServiceHost/Unity/InjectionValueBuilder.cs @ 278:6691aff01de1 v3
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
Implab.ServiceHost: rewritten TypeReference (added support for nested types), stable API
author | cin |
---|---|
date | Thu, 03 May 2018 09:59:44 +0300 |
parents | 963b17c275be |
children | 8714471e8d78 |
rev | line source |
---|---|
274 | 1 using System; |
277 | 2 using System.Collections.Generic; |
274 | 3 using System.ComponentModel; |
277 | 4 using System.Linq; |
274 | 5 using System.Xml.Serialization; |
6 using Unity.Injection; | |
7 | |
8 namespace Implab.ServiceHost.Unity { | |
9 | |
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
10 public class InjectionParameterBuilder { |
274 | 11 |
12 readonly TypeResolver m_resolver; | |
13 | |
14 public Type DefaultType { get; private set; } | |
15 | |
277 | 16 public Type ValueType { get; private set; } |
274 | 17 |
18 public object Value { get; set; } | |
19 | |
277 | 20 internal InjectionParameterValue Injection { |
274 | 21 get { |
22 if (Value != null) | |
23 return InjectionParameterValue.ToParameter(Value); | |
24 | |
25 return new InjectionParameter(ValueType, null); | |
26 } | |
27 } | |
28 | |
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
29 internal InjectionParameterBuilder(TypeResolver resolver, Type defaultType) { |
274 | 30 m_resolver = resolver; |
277 | 31 DefaultType = defaultType; |
274 | 32 } |
33 | |
277 | 34 public Type ResolveInjectedValueType(string typeSpec) { |
274 | 35 if (string.IsNullOrEmpty(typeSpec)) { |
36 if (DefaultType == null) | |
37 throw new Exception("The type must be specified"); | |
38 return DefaultType; | |
39 } | |
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
40 return m_resolver.Resolve(typeSpec, true); |
274 | 41 } |
42 | |
277 | 43 public Type ResolveType(string typeSpec) { |
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
44 return m_resolver.Resolve(typeSpec, true); |
277 | 45 } |
274 | 46 |
277 | 47 public void SetValue(Type type, object value) { |
48 ValueType = type; | |
49 Value = value; | |
50 } | |
51 | |
52 public void SetValue<T>(T value) { | |
53 SetValue(typeof(T), value); | |
54 } | |
55 | |
56 public void SetDependencyReference(Type type, string name, bool optional) { | |
57 ValueType = type; | |
58 Value = optional ? (object)new OptionalParameter(type, name) : new ResolvedParameter(type, name); | |
274 | 59 } |
60 | |
277 | 61 internal void Visit(ArrayParameterElement arrayParameter) { |
62 Type itemsType = null; | |
63 var arrayType = string.IsNullOrEmpty(arrayParameter.TypeName) ? null : ResolveType(arrayParameter.TypeName); | |
274 | 64 |
277 | 65 if (!string.IsNullOrEmpty(arrayParameter.ItemsType)) { |
66 itemsType = ResolveType(arrayParameter.ItemsType); | |
67 if (arrayType == null) | |
68 arrayType = itemsType.MakeArrayType(); | |
69 } else { | |
70 itemsType = arrayType?.GetInterface(typeof(IEnumerable<>).FullName)?.GetGenericArguments()[0]; | |
71 } | |
274 | 72 |
277 | 73 if (itemsType == null) |
74 throw new Exception("Failed to determine array elements type"); | |
274 | 75 |
277 | 76 InjectionParameterValue[] injections = (arrayParameter.Items ?? new InjectionParameterElement[0]) |
77 .Select(x => { | |
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
78 var builder = new InjectionParameterBuilder(m_resolver, itemsType); |
277 | 79 x.Visit(builder); |
80 return builder.Injection; | |
81 }) | |
82 .ToArray(); | |
83 | |
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
84 var array = itemsType.IsGenericParameter ? |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
85 (object)new GenericResolvedArrayParameter(itemsType.Name, injections) : |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
86 new ResolvedArrayParameter(itemsType, injections); |
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
277
diff
changeset
|
87 |
277 | 88 ValueType = arrayType; |
89 Value = array; | |
274 | 90 } |
91 } | |
92 } |