268
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Text.RegularExpressions;
|
|
4 using Implab.Diagnostics;
|
|
5
|
|
6 namespace Implab.ServiceHost.Unity {
|
|
7 using System.Linq;
|
269
|
8 using System.Reflection;
|
268
|
9 using System.Text;
|
269
|
10 using global::Unity;
|
268
|
11 using static Trace<ConfigurationContext>;
|
|
12
|
|
13 public class ConfigurationContext {
|
|
14 Regex _nsRx = new Regex(@"^\w+(\.\w+)*$", RegexOptions.Compiled);
|
|
15 readonly LinkedList<string> m_namespases = new LinkedList<string>();
|
|
16
|
|
17 LinkedListNode<string> m_insertAt;
|
|
18
|
269
|
19 readonly UnityContainer m_container;
|
|
20
|
|
21 public ConfigurationContext(UnityContainer container) {
|
|
22 m_container = container ?? new UnityContainer();
|
268
|
23 m_insertAt = new LinkedListNode<string>(string.Empty);
|
|
24 m_namespases.AddFirst(m_insertAt);
|
|
25 }
|
|
26
|
|
27 public void AddNamespace(string ns) {
|
|
28 Safe.ArgumentMatch(ns, nameof(ns), _nsRx);
|
|
29 if (m_insertAt != null)
|
|
30 m_namespases.AddAfter(m_insertAt, ns);
|
|
31 else
|
|
32 m_namespases.AddFirst(ns);
|
|
33 }
|
|
34
|
|
35 public Type Resolve(TypeReference reference) {
|
|
36 Safe.ArgumentNotNull(reference, nameof(reference));
|
|
37
|
|
38 var args = reference.IsGeneric && !reference.IsOpenGeneric ? reference.GenericParameters?.Select(Resolve).ToArray() : null;
|
|
39 var argc = reference.IsGeneric ? reference.GenericParameters.Length : 0;
|
|
40
|
|
41 foreach (var ns in m_namespases) {
|
|
42 var typeName = FormatName(new [] { ns, reference.Namespace, reference.TypeName}, argc, args, reference.IsArray);
|
|
43
|
269
|
44 var resolved = ProbeType(typeName);
|
268
|
45 if (resolved != null) {
|
269
|
46 Log("Probe succeed {0} in '{1}': {2} -> {3}", reference, ns, typeName, resolved.AssemblyQualifiedName);
|
268
|
47 return resolved;
|
|
48 } else {
|
|
49 Log("Probe failed {0} in '{1}': {2}", reference, ns, typeName);
|
|
50 }
|
|
51 }
|
|
52
|
|
53 throw new Exception($"Failed to resolve: {reference}");
|
|
54 }
|
|
55
|
269
|
56 Type ProbeType(string typeName) {
|
|
57 var assemblies = AppDomain.CurrentDomain.GetAssemblies();
|
|
58
|
|
59 foreach(var assembly in assemblies) {
|
|
60 var type = assembly.GetType(typeName);
|
|
61 if (type != null)
|
|
62 return type;
|
|
63 }
|
|
64 return null;
|
|
65 }
|
|
66
|
268
|
67 string FormatName(string[] parts, int argc, Type[] args, bool isArray) {
|
|
68 var builder = new StringBuilder();
|
|
69
|
|
70 builder.Append(String.Join(".", parts.Where(x => !string.IsNullOrEmpty(x))));
|
|
71 if (argc > 0) {
|
|
72 builder.Append('`');
|
|
73 builder.Append(argc);
|
|
74 }
|
|
75
|
|
76 if (args!= null && args.Length > 0) {
|
|
77 builder.Append('[');
|
269
|
78 builder.Append(string.Join(",", args.Select(x => $"[{x.AssemblyQualifiedName}]")));
|
268
|
79 builder.Append(']');
|
|
80 }
|
|
81
|
|
82 if(isArray)
|
|
83 builder.Append("[]");
|
|
84
|
|
85 return builder.ToString();
|
|
86 }
|
|
87
|
|
88 public Type Resolve(string typeReference) {
|
|
89 return Resolve(TypeReference.Parse(typeReference));
|
|
90 }
|
|
91
|
269
|
92 public void Visist(AbstractRegistration descriptor) {
|
|
93
|
|
94 }
|
|
95
|
|
96 public void Include(string file) {
|
268
|
97
|
|
98 }
|
|
99
|
|
100 }
|
|
101 } |