annotate Implab.ServiceHost/Unity/ConfigurationContext.cs @ 269:ff581cff7003 v3

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