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,4 +1,6 | |||
| 
             | 
        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; | 
| @@ -30,9 +32,20 namespace Implab.Playground { | |||
| 
             | 
        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 | |
| @@ -1,3 +1,7 | |||
| 
             | 
        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; } | 
| @@ -5,5 +9,50 namespace Implab.ServiceHost.Unity { | |||
| 
             | 
        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 | 
| @@ -3,7 +3,7 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 | |
| @@ -20,7 +20,7 namespace Implab.ServiceHost.Unity { | |||
| 
             | 
        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 | |
| @@ -28,12 +28,16 namespace Implab.ServiceHost.Unity { | |||
| 
             | 
        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; | 
| @@ -49,6 +53,7 namespace Implab.ServiceHost.Unity { | |||
| 
             | 
        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; | 
| @@ -63,6 +68,7 namespace Implab.ServiceHost.Unity { | |||
| 
             | 
        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 ",": | 
| @@ -75,8 +81,11 namespace Implab.ServiceHost.Unity { | |||
| 
             | 
        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() { | 
| @@ -118,20 +127,16 namespace Implab.ServiceHost.Unity { | |||
| 
             | 
        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 | |
| @@ -139,18 +144,19 namespace Implab.ServiceHost.Unity { | |||
| 
             | 
        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
                    
                