##// END OF EJS Templates
Working on Unity container xml configuration
Working on Unity container xml configuration

File last commit:

r269:ff581cff7003 v3
r269:ff581cff7003 v3
Show More
ConfigurationContext.cs
100 lines | 3.3 KiB | text/x-csharp | CSharpLexer
cin
Implemented typereference parser
r268 using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Implab.Diagnostics;
namespace Implab.ServiceHost.Unity {
using System.Linq;
cin
Working on Unity container xml configuration
r269 using System.Reflection;
cin
Implemented typereference parser
r268 using System.Text;
cin
Working on Unity container xml configuration
r269 using global::Unity;
cin
Implemented typereference parser
r268 using static Trace<ConfigurationContext>;
public class ConfigurationContext {
Regex _nsRx = new Regex(@"^\w+(\.\w+)*$", RegexOptions.Compiled);
readonly LinkedList<string> m_namespases = new LinkedList<string>();
LinkedListNode<string> m_insertAt;
cin
Working on Unity container xml configuration
r269 readonly UnityContainer m_container;
public ConfigurationContext(UnityContainer container) {
m_container = container ?? new UnityContainer();
cin
Implemented typereference parser
r268 m_insertAt = new LinkedListNode<string>(string.Empty);
m_namespases.AddFirst(m_insertAt);
}
public void AddNamespace(string ns) {
Safe.ArgumentMatch(ns, nameof(ns), _nsRx);
if (m_insertAt != null)
m_namespases.AddAfter(m_insertAt, ns);
else
m_namespases.AddFirst(ns);
}
public Type Resolve(TypeReference reference) {
Safe.ArgumentNotNull(reference, nameof(reference));
var args = reference.IsGeneric && !reference.IsOpenGeneric ? reference.GenericParameters?.Select(Resolve).ToArray() : null;
var argc = reference.IsGeneric ? reference.GenericParameters.Length : 0;
foreach (var ns in m_namespases) {
var typeName = FormatName(new [] { ns, reference.Namespace, reference.TypeName}, argc, args, reference.IsArray);
cin
Working on Unity container xml configuration
r269 var resolved = ProbeType(typeName);
cin
Implemented typereference parser
r268 if (resolved != null) {
cin
Working on Unity container xml configuration
r269 Log("Probe succeed {0} in '{1}': {2} -> {3}", reference, ns, typeName, resolved.AssemblyQualifiedName);
cin
Implemented typereference parser
r268 return resolved;
} else {
Log("Probe failed {0} in '{1}': {2}", reference, ns, typeName);
}
}
throw new Exception($"Failed to resolve: {reference}");
}
cin
Working on Unity container xml configuration
r269 Type ProbeType(string typeName) {
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach(var assembly in assemblies) {
var type = assembly.GetType(typeName);
if (type != null)
return type;
}
return null;
}
cin
Implemented typereference parser
r268 string FormatName(string[] parts, int argc, Type[] args, bool isArray) {
var builder = new StringBuilder();
builder.Append(String.Join(".", parts.Where(x => !string.IsNullOrEmpty(x))));
if (argc > 0) {
builder.Append('`');
builder.Append(argc);
}
if (args!= null && args.Length > 0) {
builder.Append('[');
cin
Working on Unity container xml configuration
r269 builder.Append(string.Join(",", args.Select(x => $"[{x.AssemblyQualifiedName}]")));
cin
Implemented typereference parser
r268 builder.Append(']');
}
if(isArray)
builder.Append("[]");
return builder.ToString();
}
public Type Resolve(string typeReference) {
return Resolve(TypeReference.Parse(typeReference));
}
cin
Working on Unity container xml configuration
r269 public void Visist(AbstractRegistration descriptor) {
}
public void Include(string file) {
cin
Implemented typereference parser
r268
}
}
}