TypeResolver.cs
97 lines
| 3.1 KiB
| text/x-csharp
|
CSharpLexer
cin
|
r270 | using System; | |
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Text.RegularExpressions; | |||
using Implab.Diagnostics; | |||
cin
|
r277 | namespace Implab.ServiceHost.Unity { | |
cin
|
r278 | using System.Diagnostics; | |
cin
|
r270 | using static Trace<TypeResolver>; | |
cin
|
r277 | public class TypeResolver { | |
cin
|
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
|
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
|
r270 | LinkedListNode<string> m_insertAt; | |
readonly TypeResolver m_parent; | |||
public TypeResolver() : this(null) { | |||
} | |||
public TypeResolver(TypeResolver parent) { | |||
cin
|
r272 | m_parent = parent; | |
cin
|
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
|
r278 | Type ProbeInNamespaces(string localName) { | |
cin
|
r270 | ||
Type resolved; | |||
cin
|
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
|
r270 | } | |
return resolved; | |||
} | |||
Type Probe(string typeName) { | |||
var assemblies = AppDomain.CurrentDomain.GetAssemblies(); | |||
cin
|
r277 | foreach (var assembly in assemblies) { | |
cin
|
r270 | var type = assembly.GetType(typeName); | |
if (type != null) | |||
return type; | |||
} | |||
return null; | |||
} | |||
} | |||
} |