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
|
|
10 public class InjectionValueBuilder {
|
|
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
|
277
|
29 internal InjectionValueBuilder(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 }
|
|
40 return m_resolver.Resolve(typeSpec);
|
|
41 }
|
|
42
|
277
|
43 public Type ResolveType(string typeSpec) {
|
|
44 return m_resolver.Resolve(typeSpec);
|
|
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 => {
|
|
78 var builder = new InjectionValueBuilder(m_resolver, itemsType);
|
|
79 x.Visit(builder);
|
|
80 return builder.Injection;
|
|
81 })
|
|
82 .ToArray();
|
|
83
|
|
84 var array = itemsType.IsGenericParameter ? (object)new GenericResolvedArrayParameter(itemsType.Name, injections) : new ResolvedArrayParameter(itemsType, injections);
|
|
85 ValueType = arrayType;
|
|
86 Value = array;
|
274
|
87 }
|
|
88 }
|
|
89 } |