annotate Implab.ServiceHost/Unity/ConfigurationContext.cs @ 268:0be8a6ae8307 v3

Implemented typereference parser
author cin
date Sun, 22 Apr 2018 15:29:10 +0300
parents
children ff581cff7003
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;
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
8 using System.Text;
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
9 using static Trace<ConfigurationContext>;
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
10
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
11 public class ConfigurationContext {
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
12 Regex _nsRx = new Regex(@"^\w+(\.\w+)*$", RegexOptions.Compiled);
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
13 readonly LinkedList<string> m_namespases = new LinkedList<string>();
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
14
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
15 LinkedListNode<string> m_insertAt;
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
16
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
17 public ConfigurationContext() {
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
18 m_insertAt = new LinkedListNode<string>(string.Empty);
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
19 m_namespases.AddFirst(m_insertAt);
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
20 }
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
21
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
22 public void AddNamespace(string ns) {
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
23 Safe.ArgumentMatch(ns, nameof(ns), _nsRx);
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
24 if (m_insertAt != null)
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
25 m_namespases.AddAfter(m_insertAt, ns);
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
26 else
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
27 m_namespases.AddFirst(ns);
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
28 }
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
29
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
30 public Type Resolve(TypeReference reference) {
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
31 Safe.ArgumentNotNull(reference, nameof(reference));
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
32
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
33 var args = reference.IsGeneric && !reference.IsOpenGeneric ? reference.GenericParameters?.Select(Resolve).ToArray() : null;
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
34 var argc = reference.IsGeneric ? reference.GenericParameters.Length : 0;
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
35
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
36 foreach (var ns in m_namespases) {
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
37 var typeName = FormatName(new [] { ns, reference.Namespace, reference.TypeName}, argc, args, reference.IsArray);
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
38
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
39 var resolved = Type.GetType(typeName, false);
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
40 if (resolved != null) {
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
41 Log("Probe succeed {0} in '{1}': {2} -> {3}", reference, ns, typeName, resolved.FullName);
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
42 return resolved;
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
43 } else {
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
44 Log("Probe failed {0} in '{1}': {2}", reference, ns, typeName);
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
45 }
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
46 }
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
47
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
48 throw new Exception($"Failed to resolve: {reference}");
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
49 }
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
50
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
51 string FormatName(string[] parts, int argc, Type[] args, bool isArray) {
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
52 var builder = new StringBuilder();
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
53
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
54 builder.Append(String.Join(".", parts.Where(x => !string.IsNullOrEmpty(x))));
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
55 if (argc > 0) {
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
56 builder.Append('`');
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
57 builder.Append(argc);
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
58 }
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
59
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
60 if (args!= null && args.Length > 0) {
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
61 builder.Append('[');
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
62 builder.Append(string.Join(",", args.Select(x => x.FullName)));
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
63 builder.Append(']');
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
64 }
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
65
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
66 if(isArray)
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
67 builder.Append("[]");
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
68
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
69 return builder.ToString();
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
70 }
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
71
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
72 public Type Resolve(string typeReference) {
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
73 return Resolve(TypeReference.Parse(typeReference));
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 public void Register(ServiceElement descriptor) {
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
77
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
78 }
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
79
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
80 }
0be8a6ae8307 Implemented typereference parser
cin
parents:
diff changeset
81 }