annotate Implab.ServiceHost/Unity/TypeResolver.cs @ 277:963b17c275be v3

Refactoring Added <array> element to injection parameters Working on registrations of factories
author cin
date Sat, 28 Apr 2018 18:48:09 +0300
parents 9d1cca834b05
children 6691aff01de1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
1 using System;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
2 using System.Collections.Generic;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
3 using System.Linq;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
4 using System.Text;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
5 using System.Text.RegularExpressions;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
6 using Implab.Diagnostics;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
7
277
963b17c275be Refactoring
cin
parents: 272
diff changeset
8 namespace Implab.ServiceHost.Unity {
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
9 using static Trace<TypeResolver>;
277
963b17c275be Refactoring
cin
parents: 272
diff changeset
10 public class TypeResolver {
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
11 readonly Dictionary<string, Type> m_cache = new Dictionary<string, Type>();
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
12
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
13 Regex _nsRx = new Regex(@"^\w+(\.\w+)*$", RegexOptions.Compiled);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
14 readonly LinkedList<string> m_namespases = new LinkedList<string>();
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
15
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
16 LinkedListNode<string> m_insertAt;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
17
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
18 readonly TypeResolver m_parent;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
19
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
20 public TypeResolver() : this(null) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
21
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
22 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
23
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
24 public TypeResolver(TypeResolver parent) {
272
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
25 m_parent = parent;
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
26 m_insertAt = new LinkedListNode<string>(string.Empty);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
27 m_namespases.AddFirst(m_insertAt);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
28 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
29
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
30 public void AddNamespace(string ns) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
31 Safe.ArgumentMatch(ns, nameof(ns), _nsRx);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
32 if (m_insertAt != null)
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
33 m_namespases.AddAfter(m_insertAt, ns);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
34 else
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
35 m_namespases.AddFirst(ns);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
36 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
37
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
38 public void AddMapping(string typeName, Type type) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
39 Safe.ArgumentNotEmpty(typeName, nameof(typeName));
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
40 Safe.ArgumentNotNull(type, nameof(type));
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
41
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
42 m_cache[typeName] = type;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
43 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
44
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
45 public Type Resolve(TypeReference reference) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
46 var args = reference.IsGeneric && !reference.IsOpenGeneric ? reference.GenericParameters?.Select(Resolve).ToArray() : null;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
47 var argc = reference.IsGeneric ? reference.GenericParameters.Length : 0;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
48
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
49 Type resolved;
277
963b17c275be Refactoring
cin
parents: 272
diff changeset
50 if (!m_cache.TryGetValue(reference.ToString(), out resolved)) {
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
51 resolved = ResolveInternal(reference, args, argc);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
52 if (resolved == null)
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
53 throw new Exception($"Failed to resolve {reference}");
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
54 m_cache[reference.ToString()] = resolved;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
55 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
56
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
57 return resolved;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
58 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
59
272
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
60 public Type Resolve(string typeSpec) {
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
61 return Resolve(TypeReference.Parse(typeSpec));
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
62 }
9d1cca834b05 preview version of Unity xml configuration
cin
parents: 270
diff changeset
63
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
64 Type ResolveInternal(TypeReference reference, Type[] args, int argc) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
65 var resolved = ProbeInNamespaces(
277
963b17c275be Refactoring
cin
parents: 272
diff changeset
66 String.Join(".", new[] { reference.Namespace, reference.TypeName }.Where(x => !string.IsNullOrEmpty(x))),
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
67 args,
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
68 argc,
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
69 reference.IsArray,
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
70 reference.ToString()
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
71 );
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
72
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
73 if (resolved == null && m_parent != null)
277
963b17c275be Refactoring
cin
parents: 272
diff changeset
74 resolved = m_parent.ResolveInternal(reference, args, argc);
963b17c275be Refactoring
cin
parents: 272
diff changeset
75
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
76 return resolved;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
77 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
78
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
79 public Type ProbeInNamespaces(string localName, Type[] args, int argc, bool isArray, string referenceName) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
80 foreach (var ns in m_namespases) {
277
963b17c275be Refactoring
cin
parents: 272
diff changeset
81 var typeName = FormatName(new[] { ns, localName }, argc);
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
82
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
83 var resolved = Probe(typeName);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
84 if (resolved != null) {
277
963b17c275be Refactoring
cin
parents: 272
diff changeset
85 if (args != null && args.Length > 0) {
963b17c275be Refactoring
cin
parents: 272
diff changeset
86 resolved = resolved.MakeGenericType(args);
963b17c275be Refactoring
cin
parents: 272
diff changeset
87 }
963b17c275be Refactoring
cin
parents: 272
diff changeset
88
963b17c275be Refactoring
cin
parents: 272
diff changeset
89 if (isArray)
963b17c275be Refactoring
cin
parents: 272
diff changeset
90 resolved = resolved.MakeArrayType();
963b17c275be Refactoring
cin
parents: 272
diff changeset
91
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
92 Log("Probe succeed {0} in '{1}': {2} -> {3}", referenceName, ns, typeName, resolved.AssemblyQualifiedName);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
93 return resolved;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
94 } else {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
95 Log("Probe failed {0} in '{1}': {2}", referenceName, ns, typeName);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
96 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
97 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
98
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
99 return null;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
100 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
101
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
102 Type Probe(string typeName) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
103 var assemblies = AppDomain.CurrentDomain.GetAssemblies();
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
104
277
963b17c275be Refactoring
cin
parents: 272
diff changeset
105 foreach (var assembly in assemblies) {
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
106 var type = assembly.GetType(typeName);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
107 if (type != null)
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
108 return type;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
109 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
110 return null;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
111 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
112
277
963b17c275be Refactoring
cin
parents: 272
diff changeset
113 string FormatName(string[] parts, int argc) {
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
114 var builder = new StringBuilder();
277
963b17c275be Refactoring
cin
parents: 272
diff changeset
115
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
116 builder.Append(String.Join(".", parts.Where(x => !string.IsNullOrEmpty(x))));
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
117 if (argc > 0) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
118 builder.Append('`');
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
119 builder.Append(argc);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
120 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
121
277
963b17c275be Refactoring
cin
parents: 272
diff changeset
122 return builder.ToString();
270
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
123 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
124 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
125 }