annotate Implab.ServiceHost/Unity/TypeResolver.cs @ 270:ade80d94dfb5 v3

Working on Unity container xml configuration
author cin
date Wed, 25 Apr 2018 04:44:40 +0300
parents
children 9d1cca834b05
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) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
27 m_insertAt = new LinkedListNode<string>(string.Empty);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
28 m_namespases.AddFirst(m_insertAt);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
29 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
30
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
31 public void AddNamespace(string ns) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
32 Safe.ArgumentMatch(ns, nameof(ns), _nsRx);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
33 if (m_insertAt != null)
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
34 m_namespases.AddAfter(m_insertAt, ns);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
35 else
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
36 m_namespases.AddFirst(ns);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
37 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
38
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
39 public void AddMapping(string typeName, Type type) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
40 Safe.ArgumentNotEmpty(typeName, nameof(typeName));
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
41 Safe.ArgumentNotNull(type, nameof(type));
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
42
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
43 m_cache[typeName] = type;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
44 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
45
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
46 public Type Resolve(TypeReference reference) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
47 var args = reference.IsGeneric && !reference.IsOpenGeneric ? reference.GenericParameters?.Select(Resolve).ToArray() : null;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
48 var argc = reference.IsGeneric ? reference.GenericParameters.Length : 0;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
49
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
50 Type resolved;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
51 if(!m_cache.TryGetValue(reference.ToString(), out resolved)) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
52 resolved = ResolveInternal(reference, args, argc);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
53 if (resolved == null)
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
54 throw new Exception($"Failed to resolve {reference}");
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
55 m_cache[reference.ToString()] = resolved;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
56 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
57
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
58 return resolved;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
59 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
60
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
61 Type ResolveInternal(TypeReference reference, Type[] args, int argc) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
62 var resolved = ProbeInNamespaces(
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
63 String.Join(".", new [] { reference.Namespace, reference.TypeName }.Where(x => !string.IsNullOrEmpty(x)) ),
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
64 args,
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
65 argc,
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
66 reference.IsArray,
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
67 reference.ToString()
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
68 );
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
69
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
70 if (resolved == null && m_parent != null)
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
71 resolved = m_parent.Resolve(reference);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
72
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
73 return resolved;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
74 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
75
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
76 public Type ProbeInNamespaces(string localName, Type[] args, int argc, bool isArray, string referenceName) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
77 foreach (var ns in m_namespases) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
78 var typeName = FormatName(new [] { ns, localName}, argc, args, isArray);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
79
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
80 var resolved = Probe(typeName);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
81 if (resolved != null) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
82 Log("Probe succeed {0} in '{1}': {2} -> {3}", referenceName, ns, typeName, resolved.AssemblyQualifiedName);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
83 return resolved;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
84 } else {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
85 Log("Probe failed {0} in '{1}': {2}", referenceName, ns, typeName);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
86 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
87 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
88
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
89 return null;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
90 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
91
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
92 Type Probe(string typeName) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
93 var assemblies = AppDomain.CurrentDomain.GetAssemblies();
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
94
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
95 foreach(var assembly in assemblies) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
96 var type = assembly.GetType(typeName);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
97 if (type != null)
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
98 return type;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
99 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
100 return null;
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
101 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
102
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
103 string FormatName(string[] parts, int argc, Type[] args, bool isArray) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
104 var builder = new StringBuilder();
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
105
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
106 builder.Append(String.Join(".", parts.Where(x => !string.IsNullOrEmpty(x))));
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
107 if (argc > 0) {
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
108 builder.Append('`');
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
109 builder.Append(argc);
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
110 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
111
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
112 if (args!= null && args.Length > 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(string.Join(",", args.Select(x => $"[{x.AssemblyQualifiedName}]")));
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
115 builder.Append(']');
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
116 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
117
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
118 if(isArray)
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
119 builder.Append("[]");
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
120
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
121 return builder.ToString();
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
122 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
123 }
ade80d94dfb5 Working on Unity container xml configuration
cin
parents:
diff changeset
124 }