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