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