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