Auto status change to "Under Review"
@@ -0,0 +1,81 | |||
|
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; | |
|
8 | using System.Text; | |
|
9 | using static Trace<ConfigurationContext>; | |
|
10 | ||
|
11 | public class ConfigurationContext { | |
|
12 | Regex _nsRx = new Regex(@"^\w+(\.\w+)*$", RegexOptions.Compiled); | |
|
13 | readonly LinkedList<string> m_namespases = new LinkedList<string>(); | |
|
14 | ||
|
15 | LinkedListNode<string> m_insertAt; | |
|
16 | ||
|
17 | public ConfigurationContext() { | |
|
18 | m_insertAt = new LinkedListNode<string>(string.Empty); | |
|
19 | m_namespases.AddFirst(m_insertAt); | |
|
20 | } | |
|
21 | ||
|
22 | public void AddNamespace(string ns) { | |
|
23 | Safe.ArgumentMatch(ns, nameof(ns), _nsRx); | |
|
24 | if (m_insertAt != null) | |
|
25 | m_namespases.AddAfter(m_insertAt, ns); | |
|
26 | else | |
|
27 | m_namespases.AddFirst(ns); | |
|
28 | } | |
|
29 | ||
|
30 | public Type Resolve(TypeReference reference) { | |
|
31 | Safe.ArgumentNotNull(reference, nameof(reference)); | |
|
32 | ||
|
33 | var args = reference.IsGeneric && !reference.IsOpenGeneric ? reference.GenericParameters?.Select(Resolve).ToArray() : null; | |
|
34 | var argc = reference.IsGeneric ? reference.GenericParameters.Length : 0; | |
|
35 | ||
|
36 | foreach (var ns in m_namespases) { | |
|
37 | var typeName = FormatName(new [] { ns, reference.Namespace, reference.TypeName}, argc, args, reference.IsArray); | |
|
38 | ||
|
39 | var resolved = Type.GetType(typeName, false); | |
|
40 | if (resolved != null) { | |
|
41 | Log("Probe succeed {0} in '{1}': {2} -> {3}", reference, ns, typeName, resolved.FullName); | |
|
42 | return resolved; | |
|
43 | } else { | |
|
44 | Log("Probe failed {0} in '{1}': {2}", reference, ns, typeName); | |
|
45 | } | |
|
46 | } | |
|
47 | ||
|
48 | throw new Exception($"Failed to resolve: {reference}"); | |
|
49 | } | |
|
50 | ||
|
51 | string FormatName(string[] parts, int argc, Type[] args, bool isArray) { | |
|
52 | var builder = new StringBuilder(); | |
|
53 | ||
|
54 | builder.Append(String.Join(".", parts.Where(x => !string.IsNullOrEmpty(x)))); | |
|
55 | if (argc > 0) { | |
|
56 | builder.Append('`'); | |
|
57 | builder.Append(argc); | |
|
58 | } | |
|
59 | ||
|
60 | if (args!= null && args.Length > 0) { | |
|
61 | builder.Append('['); | |
|
62 | builder.Append(string.Join(",", args.Select(x => x.FullName))); | |
|
63 | builder.Append(']'); | |
|
64 | } | |
|
65 | ||
|
66 | if(isArray) | |
|
67 | builder.Append("[]"); | |
|
68 | ||
|
69 | return builder.ToString(); | |
|
70 | } | |
|
71 | ||
|
72 | public Type Resolve(string typeReference) { | |
|
73 | return Resolve(TypeReference.Parse(typeReference)); | |
|
74 | } | |
|
75 | ||
|
76 | public void Register(ServiceElement descriptor) { | |
|
77 | ||
|
78 | } | |
|
79 | ||
|
80 | } | |
|
81 | } No newline at end of file |
@@ -1,40 +1,53 | |||
|
1 | 1 | using System; |
|
2 | using System.Diagnostics; | |
|
3 | using Implab.Diagnostics; | |
|
2 | 4 | using Implab.ServiceHost.Unity; |
|
3 | 5 | using Implab.Xml; |
|
4 | 6 | using Unity; |
|
5 | 7 | using Unity.Injection; |
|
6 | 8 | |
|
7 | 9 | namespace Implab.Playground { |
|
8 | 10 | |
|
9 | 11 | public class Foo { |
|
10 | 12 | public int IntValue { get; set; } |
|
11 | 13 | |
|
12 | 14 | public string StringValue { get; set; } |
|
13 | 15 | |
|
14 | 16 | } |
|
15 | 17 | |
|
16 | 18 | public class Container<T> { |
|
17 | 19 | public Container() { |
|
18 | 20 | |
|
19 | 21 | } |
|
20 | 22 | |
|
21 | 23 | public Container(T instance) { |
|
22 | 24 | Instance = instance; |
|
23 | 25 | } |
|
24 | 26 | |
|
25 | 27 | public T Instance { get; set; } |
|
26 | 28 | } |
|
27 | 29 | |
|
28 | 30 | public class Program { |
|
29 | 31 | |
|
30 | 32 | static void Main(string[] args) { |
|
31 | 33 | var container = new UnityContainer(); |
|
32 | 34 | |
|
33 | var containerConfig = SerializationHelpers.DeserializeFromFile<ContainerElement>("data/sample.xml"); | |
|
35 | var listener = new SimpleTraceListener(Console.Out); | |
|
36 | Trace<ConfigurationContext>.TraceSource.Switch.Level = SourceLevels.All; | |
|
37 | Trace<ConfigurationContext>.TraceSource.Listeners.Add(listener); | |
|
38 | ||
|
39 | var c = new Container<int>(); | |
|
34 | 40 | |
|
35 | Console.WriteLine($"container: {containerConfig.Registrations.Count}"); | |
|
41 | var cts = new ConfigurationContext(); | |
|
42 | cts.AddNamespace("System"); | |
|
43 | cts.AddNamespace("System.Collections.Generic"); | |
|
44 | cts.AddNamespace("Implab.Playground"); | |
|
45 | ||
|
46 | Console.WriteLine(c.GetType().FullName); | |
|
47 | ||
|
48 | cts.Resolve("Container{Int32}"); | |
|
36 | 49 | } |
|
37 | 50 | |
|
38 | 51 | |
|
39 | 52 | } |
|
40 | 53 | } |
@@ -1,9 +1,58 | |||
|
1 | using System; | |
|
2 | using System.Linq; | |
|
3 | using System.Text; | |
|
4 | ||
|
1 | 5 | namespace Implab.ServiceHost.Unity { |
|
2 | 6 | public class TypeReference { |
|
3 | 7 | public string TypeName { get; set; } |
|
4 | 8 | |
|
5 | 9 | public string Namespace { get; set; } |
|
6 | 10 | |
|
7 | 11 | public TypeReference[] GenericParameters { get; set; } |
|
12 | ||
|
13 | public bool IsArray { get; set; } | |
|
14 | ||
|
15 | public bool IsOpenGeneric { | |
|
16 | get { | |
|
17 | return GenericParameters!=null && GenericParameters.Contains(null); | |
|
18 | } | |
|
19 | } | |
|
20 | ||
|
21 | public bool IsGeneric { | |
|
22 | get { | |
|
23 | return GenericParameters != null && GenericParameters.Length > 0; | |
|
24 | } | |
|
25 | } | |
|
26 | ||
|
27 | public override string ToString() { | |
|
28 | var builder = new StringBuilder(); | |
|
29 | ||
|
30 | if (!string.IsNullOrEmpty(Namespace)) { | |
|
31 | builder.Append(Namespace); | |
|
32 | builder.Append('.'); | |
|
33 | } | |
|
34 | ||
|
35 | if (!string.IsNullOrEmpty(TypeName)) { | |
|
36 | builder.Append(TypeName); | |
|
37 | } else { | |
|
38 | builder.Append("__unnamed__"); | |
|
39 | } | |
|
40 | ||
|
41 | if (GenericParameters != null && GenericParameters.Length > 0) { | |
|
42 | builder.Append('{'); | |
|
43 | for(var i = 0; i < GenericParameters.Length; i++) { | |
|
44 | if (i > 0) | |
|
45 | builder.Append(','); | |
|
46 | builder.Append(GenericParameters[i]); | |
|
47 | } | |
|
48 | builder.Append('}'); | |
|
49 | } | |
|
50 | ||
|
51 | return builder.ToString(); | |
|
52 | } | |
|
53 | public static TypeReference Parse(string text) { | |
|
54 | var parser = new TypeReferenceParser(text); | |
|
55 | return parser.Parse(); | |
|
56 | } | |
|
8 | 57 | } |
|
9 | 58 | } No newline at end of file |
@@ -1,156 +1,162 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Text.RegularExpressions; |
|
4 | 4 | |
|
5 | 5 | namespace Implab.ServiceHost.Unity { |
|
6 |
|
|
|
6 | internal class TypeReferenceParser { | |
|
7 | 7 | enum TokenType { |
|
8 | 8 | None, |
|
9 | 9 | |
|
10 | 10 | Word, |
|
11 | 11 | |
|
12 | 12 | Dot, |
|
13 | 13 | |
|
14 | 14 | Comma, |
|
15 | 15 | |
|
16 | 16 | OpenList, |
|
17 | 17 | |
|
18 | 18 | CloseList, |
|
19 | 19 | |
|
20 | 20 | Eof |
|
21 | 21 | } |
|
22 | 22 | |
|
23 | readonly Regex _tokens = new Regex(@"(\w+)|\s*([\.{},])\s*"); | |
|
23 | readonly Regex _tokens = new Regex(@"(\w+)|\s*([\.{},\+])\s*"); | |
|
24 | 24 | |
|
25 | 25 | TokenType m_token; |
|
26 | 26 | |
|
27 | 27 | string m_tokenValue; |
|
28 | 28 | |
|
29 | 29 | int m_pos; |
|
30 | 30 | |
|
31 | int m_tokenPos; | |
|
32 | ||
|
31 | 33 | readonly string m_text; |
|
32 | 34 | |
|
33 | 35 | TokenType Token { get { return m_token; } } |
|
34 | 36 | |
|
35 | 37 | string TokenValue { get { return m_tokenValue; } } |
|
36 | 38 | |
|
39 | int TokenPos { get { return m_tokenPos; } } | |
|
40 | ||
|
37 | 41 | public TypeReferenceParser(string text) { |
|
38 | 42 | Safe.ArgumentNotEmpty(text, nameof(text)); |
|
39 | 43 | m_text = text; |
|
40 | 44 | } |
|
41 | 45 | |
|
42 | 46 | bool ReadToken() { |
|
43 | 47 | if (m_pos >= m_text.Length) { |
|
44 | 48 | m_token = TokenType.Eof; |
|
45 | 49 | m_tokenValue = null; |
|
46 | 50 | return false; |
|
47 | 51 | } |
|
48 | 52 | |
|
49 | 53 | var m = _tokens.Match(m_text, m_pos); |
|
50 | 54 | |
|
51 | 55 | if (m.Success) { |
|
56 | m_tokenPos = m_pos; | |
|
52 | 57 | m_pos += m.Length; |
|
53 | 58 | if (m.Groups[1].Success) { |
|
54 | 59 | m_token = TokenType.Word; |
|
55 | 60 | m_tokenValue = m.Groups[1].Value; |
|
56 | 61 | } else if (m.Groups[2].Success) { |
|
57 | 62 | m_tokenValue = null; |
|
58 | 63 | switch (m.Groups[2].Value) { |
|
59 | 64 | case "{": |
|
60 | 65 | m_token = TokenType.OpenList; |
|
61 | 66 | break; |
|
62 | 67 | case "}": |
|
63 | 68 | m_token = TokenType.CloseList; |
|
64 | 69 | break; |
|
65 | 70 | case ".": |
|
71 | case "+": | |
|
66 | 72 | m_token = TokenType.Dot; |
|
67 | 73 | break; |
|
68 | 74 | case ",": |
|
69 | 75 | m_token = TokenType.Comma; |
|
70 | 76 | break; |
|
71 | 77 | } |
|
72 | 78 | } |
|
73 | 79 | return true; |
|
74 | 80 | } |
|
75 | 81 | throw new FormatException($"Failed to parse '{m_text}' at pos {m_pos}"); |
|
76 | 82 | } |
|
77 | 83 | |
|
78 |
public TypeRe |
|
|
79 | ||
|
84 | public TypeReference Parse() { | |
|
85 | var result = ReadTypeReference(); | |
|
86 | if (ReadToken()) | |
|
87 | ThrowUnexpectedToken(); | |
|
88 | return result; | |
|
80 | 89 | } |
|
81 | 90 | |
|
82 | 91 | string[] ReadTypeName() { |
|
83 | 92 | var parts = new List<string>(); |
|
84 | 93 | |
|
85 | 94 | string current = null; |
|
86 | 95 | bool stop = false; |
|
87 | 96 | while ((!stop) && ReadToken()) { |
|
88 | 97 | switch (Token) { |
|
89 | 98 | case TokenType.Word: |
|
90 | 99 | if (current != null) |
|
91 | 100 | ThrowUnexpectedToken(); |
|
92 | 101 | current = TokenValue; |
|
93 | 102 | break; |
|
94 | 103 | case TokenType.Dot: |
|
95 | 104 | if (current == null) |
|
96 | 105 | ThrowUnexpectedToken(); |
|
97 | 106 | parts.Add(current); |
|
98 | 107 | current = null; |
|
99 | 108 | break; |
|
100 | 109 | default: |
|
101 | 110 | stop = true; |
|
102 | 111 | break; |
|
103 | 112 | } |
|
104 | 113 | } |
|
105 | 114 | if (current != null) |
|
106 | 115 | parts.Add(current); |
|
107 | 116 | |
|
108 | 117 | if (parts.Count == 0) |
|
109 | 118 | return null; |
|
110 | 119 | |
|
111 | 120 | return parts.ToArray(); |
|
112 | 121 | } |
|
113 | 122 | |
|
114 | 123 | TypeReference ReadTypeReference() { |
|
115 | 124 | |
|
116 | 125 | var parts = ReadTypeName(); |
|
117 | 126 | if (parts == null) |
|
118 | 127 | return null; |
|
119 | 128 | |
|
120 | 129 | var typeReference = new TypeReference { |
|
121 |
|
|
|
122 |
|
|
|
123 |
|
|
|
130 | Namespace = string.Join(".", parts, 0, parts.Length - 1), | |
|
131 | TypeName = parts[parts.Length - 1] | |
|
132 | }; | |
|
124 | 133 | |
|
125 | 134 | switch (Token) { |
|
126 | case TokenType.Eof: | |
|
127 | break; | |
|
128 | 135 | case TokenType.OpenList: |
|
129 | 136 | typeReference.GenericParameters = ReadTypeReferenceList(); |
|
130 | if(Token != TokenType.CloseList) | |
|
137 | if (Token != TokenType.CloseList) | |
|
131 | 138 | ThrowUnexpectedToken(); |
|
132 |
|
|
|
133 | default: | |
|
134 | ThrowUnexpectedToken(); | |
|
139 | ReadToken(); | |
|
135 | 140 | break; |
|
136 | 141 | } |
|
137 | 142 | |
|
138 | 143 | return typeReference; |
|
139 | 144 | } |
|
140 | 145 | |
|
141 | 146 | TypeReference[] ReadTypeReferenceList() { |
|
142 | throw new NotImplementedException(); | |
|
143 | } | |
|
147 | var list = new List<TypeReference>(); | |
|
144 | 148 | |
|
145 | void ReadDot() { | |
|
146 | if (!ReadToken() || Token != TokenType.Dot) | |
|
147 | ThrowUnexpectedToken(); | |
|
149 | do { | |
|
150 | var typeReference = ReadTypeReference(); | |
|
151 | list.Add(typeReference); | |
|
152 | } while (Token == TokenType.Comma); | |
|
153 | ||
|
154 | return list.ToArray(); | |
|
148 | 155 | } |
|
149 | 156 | |
|
150 | 157 | void ThrowUnexpectedToken() { |
|
151 |
throw new FormatException($"Unexpected '{Token}' at |
|
|
158 | throw new FormatException($"Unexpected '{Token}' at pos {TokenPos}: -->{m_text.Substring(TokenPos, Math.Min(m_text.Length - TokenPos, 10))}"); | |
|
152 | 159 | } |
|
153 | 160 | |
|
154 | ||
|
155 | 161 | } |
|
156 | 162 | } No newline at end of file |
General Comments 3
ok, latest stable version should be in default
You need to be logged in to leave comments.
Login now