annotate Implab.ServiceHost/Unity/TypeResolver.cs @ 272:9d1cca834b05 v3

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