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