##// END OF EJS Templates
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete) Implab.ServiceHost: rewritten TypeReference (added support for nested types), stable API

File last commit:

r278:6691aff01de1 v3
r278:6691aff01de1 v3
Show More
TypeResolver.cs
97 lines | 3.1 KiB | text/x-csharp | CSharpLexer
cin
Working on Unity container xml configuration
r270 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Implab.Diagnostics;
cin
Refactoring...
r277 namespace Implab.ServiceHost.Unity {
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278 using System.Diagnostics;
cin
Working on Unity container xml configuration
r270 using static Trace<TypeResolver>;
cin
Refactoring...
r277 public class TypeResolver {
cin
Working on Unity container xml configuration
r270 readonly Dictionary<string, Type> m_cache = new Dictionary<string, Type>();
Regex _nsRx = new Regex(@"^\w+(\.\w+)*$", RegexOptions.Compiled);
readonly LinkedList<string> m_namespases = new LinkedList<string>();
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278 internal Type Resolve(string ns, string typeName) {
var fullName = string.IsNullOrEmpty(ns) ? typeName : $"{ns}.{typeName}";
return ProbeInNamespaces(fullName);
}
public Type Resolve(TypeReference typeReference, bool throwOnFail) {
var context = new TypeResolutionContext(this, throwOnFail);
typeReference.Visit(context);
return context.MakeType();
}
public Type Resolve(string typeSpec, bool throwOnFail) {
var typeReference = TypeReference.Parse(typeSpec);
return Resolve(typeReference, throwOnFail);
}
cin
Working on Unity container xml configuration
r270 LinkedListNode<string> m_insertAt;
readonly TypeResolver m_parent;
public TypeResolver() : this(null) {
}
public TypeResolver(TypeResolver parent) {
cin
preview version of Unity xml configuration
r272 m_parent = parent;
cin
Working on Unity container xml configuration
r270 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 void AddMapping(string typeName, Type type) {
Safe.ArgumentNotEmpty(typeName, nameof(typeName));
Safe.ArgumentNotNull(type, nameof(type));
m_cache[typeName] = type;
}
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278 Type ProbeInNamespaces(string localName) {
cin
Working on Unity container xml configuration
r270
Type resolved;
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278 if (!m_cache.TryGetValue(localName, out resolved)) {
foreach (var ns in m_namespases) {
var typeName = string.IsNullOrEmpty(ns) ? localName : $"{ns}.{localName}";
resolved = Probe(typeName);
if (resolved != null) {
Log($"Probe '{localName}' -> '{resolved.FullName}'");
break;
}
}
if (resolved == null && m_parent != null)
resolved = m_parent.ProbeInNamespaces(localName);
if(resolved == null)
Log($"Probe '{localName}' failed");
m_cache[localName] = resolved;
cin
Working on Unity container xml configuration
r270 }
return resolved;
}
Type Probe(string typeName) {
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
cin
Refactoring...
r277 foreach (var assembly in assemblies) {
cin
Working on Unity container xml configuration
r270 var type = assembly.GetType(typeName);
if (type != null)
return type;
}
return null;
}
}
}